hdu1272 小希的迷宫 并查集
2015-05-07 22:26
429 查看
并查集不多说,这道题主要注意
1.坑爹的输入输出,0 0要输出一个Yes
2.判断是森林还是树。我的判断方法是看点和边的数量关系。节点数-1 == 边数 是为森林的必要条件
1.坑爹的输入输出,0 0要输出一个Yes
2.判断是森林还是树。我的判断方法是看点和边的数量关系。节点数-1 == 边数 是为森林的必要条件
#include<iostream> #include<iomanip> #include<cstdio> #include<cstring> #include<sstream> #include<stack> #include<queue> #include<fstream> #include<algorithm> #include<map> #include<set> #include<vector> #include<cmath> #define CLR(x) memset(x,0,sizeof(x)) #define ll long long #define eps 1e-6 using namespace std; bool visited[1111111]; #define MAXPOINTS 1111111 int n; void sinit(); int sfind(int a); int pre[MAXPOINTS]; void sinit() { for (int i=0;i<n;i++) pre[i]=i; } int sfind(int a) { if (a!=pre[a]) pre[a]=sfind(pre[a]); return pre[a]; } bool smix(int a,int b) //当前两点已经在同集中返回false { int sa(sfind(a)),sb(sfind(b)); if (sa==sb) return false; pre[sb]=sa; return true; } int main() { cin.sync_with_stdio(false); //fstream T("test.txt"); //cin.rdbuf(T.rdbuf()); int a,b; n=1111111; while (cin>>a>>b,a!=-1||b!=-1) { if (a==0&&b==0) { cout<<"Yes"<<endl; continue; } int e(1); int p(2); CLR(visited); sinit(); smix(a,b); visited[a]=visited[b]=true; bool flag(false); while (cin>>a>>b,a!=0||b!=0) { e++; if (!visited[a]) { visited[a]=true; p++; } if (!visited[b]) { visited[b]=true; p++; } if (!flag&&!smix(a,b)) flag=true; } if (!flag&&e==p-1) cout<<"Yes"<<endl; else cout<<"No"<<endl; } return 0; }
相关文章推荐
- hdu 1272 小希的迷宫(并查集 最小生成树)
- HDU1272 小希的迷宫(并查集)
- hdu 1272 小希的迷宫 【并查集】
- hdu1272小希的迷宫(并查集)
- HDU 1272 小希的迷宫 并查集 (判断任意2个点是否有且仅有一条路径可以相通)
- HDU 1272 小希的迷宫 并查集问题
- (step5.1.6)hdu 1272(小希的迷宫——并查集)
- (并查集)【HDU-1272】小希的迷宫
- 【并查集】hdu 1272 小希的迷宫
- HDU 1272 小希的迷宫(基础并查集)
- 并查集 HDU 1272 小希的迷宫
- hdu1272 小希的迷宫 (并查集)
- HDU1272 小希的迷宫 并查集
- HDU 1272 小希的迷宫 + 1325 Is It A Tree? , 并查集
- 用并查集检查连通性~HDU 1272 小希的迷宫(并查集)
- HDU 1272 小希的迷宫 并查集 (判断任意2个点是否有且仅有一条路径可以相通)
- HDU - 1272 小希的迷宫 并查集集合数判断
- [ACM] hdu 1272 小希的迷宫(并查集)
- HDU 1272 小希的迷宫(并查集)
- HDU 1272 小希的迷宫 (并查集)