题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=1272

思路

只需要判断 这张图 无环 并且只有一个连通块 就可以了

要注意 如果 只输入 0 0 那给的是空图 要给出 Yes

判断 无环 有两个方法

0.在并查集的合并操作中 如果 find(x) == find(y) 就说明 x 和 y 已经有一条边了 再加 就是成环了 直接 flag = 0

1.我们可以对边的个数 计数 只有 边数== 结点个数-1 才是满足条件的

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-30; const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
const int MOD = 1e9 + 7; int pre[maxn]; int find(int x)
{
int r = x;
while (pre[r] != r)
r = pre[r];
int i = x, j;
while (i != r)
{
j = pre[i];
pre[i] = r;
i = j;
}
return r;
} void join(int x, int y)
{
int fx = find(x), fy = find(y);
if (x != fy)
pre[fx] = fy;
} void init()
{
for (int i = 0; i < maxn; i++)
pre[i] = i;
} int main()
{
int x, y;
set <int> s;
s.clear();
int edge = 0;
init();
while (scanf("%d%d", &x, &y))
{
if (x == -1 && y == -1)
break;
else if ((x || y))
{
edge++;
s.insert(x);
s.insert(y);
join(x, y);
}
else
{
if (edge == 0)
{
printf("Yes\n");
continue;
}
map <int, int> m;
int len = s.size();
while (!s.empty())
{
int num = find(*s.begin());
s.erase(s.begin());
m[num]++;
}
int flag = 1;
if (edge != len - 1 || m.size() != 1)
flag = 0;
if (flag)
printf("Yes\n");
else
printf("No\n");
s.clear();
edge = 0;
init();
}
}
}

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-30; const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
const int MOD = 1e9 + 7; int pre[maxn]; int flag; int find(int x) //路径压缩
{
int r = x;
while (pre[r] != r)
r = pre[r];
int i = x, j;
while (i != r)
{
j = pre[i];
pre[i] = r;
i = j;
}
return r;
} //int find(int x)
//{
// while (x != pre[x])
// x = pre[x];
// return x;
//} void join(int x, int y)
{
int fx = find(x), fy = find(y);
if (fx != fy)
pre[fx] = fy;
else
flag = 0;
} void init()
{
for (int i = 0; i < maxn; i++)
pre[i] = i;
} int main()
{
int x, y;
set <int> s;
s.clear();
init();
flag = 1;
while (scanf("%d%d", &x, &y))
{
if (x == -1 && y == -1)
break;
else if ((x || y))
{
s.insert(x);
s.insert(y);
join(x, y);
}
else
{
if (s.size() == 0)
{
printf("Yes\n");
continue;
}
map <int, int> m;
int len = s.size();
while (!s.empty())
{
int num = find(*s.begin());
s.erase(s.begin());
m[num]++;
}
if (m.size() != 1)
flag = 0;
if (flag)
printf("Yes\n");
else
printf("No\n");
s.clear();
init();
flag = 1;
}
}
}

最新文章

  1. salesforce 零基础学习(二十四)解析csv格式内容
  2. html之内联元素与块状元素;
  3. Unique Binary Search Tree
  4. MongoDB入门解析【学习记录】
  5. 【有意思的BUG】分享按钮 分享功能
  6. Spring Boot应用 打包与部署指南
  7. Python 3 利用机器学习模型 进行手写体数字识别
  8. 【一天一道LeetCode】#242. Valid Anagram
  9. centos上发布部署python的tornado网站项目完整流程
  10. 静态代码扫描之阿里java代码规范IDEA插件
  11. CentOS 配置防火墙操作实例
  12. JavaSE 初学系统托盘图标SystemTray类
  13. Oracle 12c 安装问题及解决方案
  14. 解决HTTP status code is not handled or not allowed
  15. linux查看系统状态的命令
  16. Android学习笔记九:Service
  17. Unity主线程和子线程跳转调用(2)
  18. Enterprise Library 4.1 参考源码索引
  19. C++ 11 - STL - 函数对象(Function Object) (上)
  20. 利用Instrument Leak来发现App中的内存泄露

热门文章

  1. TCP应用程序通信协议的处理
  2. window.location网页URL信息
  3. Verilog利用$fdisplay命令往文件中写入数据
  4. Heterogeneity Wins
  5. java比较字符串长度
  6. linux内核中mtd架构分析
  7. vmware workstation导出ovf
  8. centos7.2 开发 部署 .net core
  9. elk文件
  10. rsync客户端命令使用简介