URAL 1320 Graph Decomposition(并查集)
2016-10-22 17:24
435 查看
1320. Graph Decomposition
Time limit: 0.5 secondMemory limit: 64 MB
There
is a simple graph with an even number of edges. You are to define if it
is possible to present it by the set of pairs of adjacent edges (having
a common vertex).
Input
containsa sequence of the numbers pairs. Each pair denotes vertices identifiers
of one edge. All the identifiers are integers from 1 to 1000. You may
assume that there are no loops and multiple edges in the graph defined
by the input data.
Output
“1” (without quotation marks), if the decomposition is possible and “0” otherwise.Samples
input | output |
---|---|
1 2 2 3 3 1 1 10 | 1 |
1 2 | 0 |
【分析】每次删除相邻的两条边,问是否能全部删除干净。偶数条边,每条边两个顶点,也就是说如果能删除干净,则每个连通块中所有点的度数之和能够整除4.
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <queue> #include <vector> #define inf 0x3f3f3f3f #define met(a,b) memset(a,b,sizeof a) typedef long long ll; using namespace std; const int N = 1005; const int M = 24005; int edg ,vis ; int in ,parent ,n; int Find(int x) { if(parent[x]!=x)parent[x]=Find(parent[x]); return parent[x]; } void Union(int x,int y) { x=Find(x);y=Find(y); if(x==y)return; parent[y]=x; } int main() { int u,v,num=1; for(int i=1;i<N;i++)parent[i]=i;n=0; while(~scanf("%d%d",&u,&v)){ Union(u,v); in[u]++;in[v]++; n=max(n,max(u,v)); } bool ok=true; for(int i=1;i<=n;i++){ vis[Find(i)]+=in[i]; } for(int i=1;i<=n;i++){ if(vis[Find(i)]%4!=0)ok=false; } if(ok)puts("1"); else puts("0"); return 0; }
相关文章推荐
- Ural 1320. Graph Decomposition(并查集,连通分支边数)
- URAL 1320 Graph Decomposition
- Ural_1671. Anansi's Cobweb(并查集)
- Ural 1671. Anansi's Cobweb(并查集)
- Cycling Roads URAL 1966 线段相交 + 并查集
- Ural 1671. Anansi's Cobweb(并查集)
- Ural 1003 Parity(并查集)
- URAL1962:In Chinese Restaurant(并查集)
- URAL(timus)1709 Penguin-Avia(并查集)
- URAL 2055 Urban Geography 分治,并查集
- ural 1003 Parity 并查集
- Ural 1682 Crazy Professor (并查集)
- URAL 1671 Anansi's Cobweb (并查集)
- URAL1671 Anansi's Cobweb(离线做 + 并查集)
- [最小极差生成树 LCT || 二分答案 CDQ分治 并查集] Ural 2055 Urban Geography
- Ural_1003 Parity(并查集)
- URAL 1982. Electrification Plan(并查集)
- URAL1671 Anansi's Cobweb(离线做 + 并查集)
- URAL 1682 Crazy Professor (并查集)
- URAL 1966 Cycling Roads 点在线段上、线段是否相交、并查集