uva 1197
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <ctype.h>
#define LL long long
#define ULL unsigned long long
#define mod 1000000007
#define INF 0x7ffffff
#define mem(a,b) memset(a,b,sizeof(a))
#define MODD(a,b) (((a%b)+b)%b)
using namespace std;
const int N=35005;
int p[N],num[N],a[N];
int n,m;
int findfa(int x)
{
if(x!=p[x])
p[x]=findfa(p[x]);
return p[x];
}
void Union(int x,int y)
{
int fax=findfa(x),fay=findfa(y);
if(fax!=fay){
p[fay]=fax;
num[fax]+=num[fay];
}
return ;
}
int main()
{
while(scanf("%d%d",&n,&m),n||m){
for(int i=0;i<n;i++) p[i]=i,num[i]=1;
while(m--){
int x;
scanf("%d",&x);
for(int i=0;i<x;i++) scanf("%d",&a[i]);
for(int i=0;i<x-1;i++) Union(a[i],a[i+1]);
}
int sum=findfa(0);
printf("%d\n",num[sum]);
// mem(p,0),mem(num,0),mem(a,0);
}
return 0;
}
hdu1272
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <ctype.h>
#define LL long long
#define ULL unsigned long long
#define mod 1000000007
#define INF 0x7ffffff
#define mem(a,b) memset(a,b,sizeof(a))
#define MODD(a,b) (((a%b)+b)%b)
using namespace std;
const int N=100005;
int p[N],r[N];
int findfa(int x)
{
if(x!=p[x])
p[x]=findfa(p[x]);
return p[x];
}
int Union(int x,int y)
{
int fax=findfa(x),fay=findfa(y);
if(fax==fay) return 1;
p[fay]=fax;
return 0;
}
int main()
{
int a,b,cot=0,ans=0;
for(int i=0;i<=N;i++) p[i]=i;
mem(r,0);
while(~scanf("%d",&a)){
scanf("%d",&b);
if(a==0&&b==0){
for(int i=1;i<=N;i++){
if(p[i]==i&&r[i])
ans++;
}
if(ans==0&&a==0&&b==0) r[a]++,r[b]++;
if((!cot&&ans==1)||(r[0]==2)) printf("Yes\n");
else if(cot||ans>1) printf("No\n");
for(int i=1;i<=N;i++) p[i]=i;
cot=0,ans=0;
mem(r,0);
continue;
}
else if(a==-1&&b==-1) return 0;
r[a]++,r[b]++;
if(cot!=1) cot=Union(a,b);
else Union(a,b);
}
return 0;
}
hdu1231
Online Judge Online Exercise Online Teaching Online Contests Exercise Author
F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
C/C++/Java Exams
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests
DIY | Web-DIY beta
Recent Contests
Author yangftc
Mail Mail 0(0)
Control Panel Control Panel
Sign Out Sign Out
View Code
Problem : 1213 ( How Many Tables ) Judge Status : Accepted
RunId : 27165582 Language : G++ Author : yangftc
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <ctype.h>
#define LL long long
#define ULL unsigned long long
#define mod 1000000007
#define INF 0x7ffffff
#define mem(a,b) memset(a,b,sizeof(a))
#define MODD(a,b) (((a%b)+b)%b)
using namespace std;
const int N=1000005;
int p[N],r[N];
int findfa(int x)
{
if(x!=p[x]){
int fa=findfa(p[x]);
p[x]=fa;
}
return p[x];
}
void Union(int x,int y)
{
int fax=findfa(x),fay=findfa(y);
if(fax==fay){
return ;
}
p[fay]=fax;
r[fay]+=r[fax];
}
int main()
{
int T;
int cot=1;
scanf("%d",&T);
while(T--){
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) p[i]=i,r[i]=1;
for(int i=0;i<m;i++){
int x,y;
scanf("%d%d",&x,&y);
Union(x,y);
}
int maxn=0;
for(int i=1;i<=n;i++){
if(p[i]==i) maxn++;
}
printf("%d\n",maxn);
}
return 0;
}
[ Copy to Clipboard ] [ Save to File]
Home | Top Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2018 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2018-11-13 22:53:56, Gzip enabled Administration
hdu 1829
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <ctype.h>
#define LL long long
#define ULL unsigned long long
#define mod 1000000007
#define INF 0x7ffffff
#define mem(a,b) memset(a,b,sizeof(a))
#define MODD(a,b) (((a%b)+b)%b)
using namespace std;
const int N=1000005;
int p[N],r[N];
bool flag;
int findfa(int x)
{
if(x!=p[x]){
int fa=findfa(p[x]);
r[x]=(r[p[x]]+r[x])&1;
p[x]=fa;
}
return p[x];
}
void Union(int x,int y)
{
int fax=findfa(x),fay=findfa(y);
if(fax==fay){
if(r[x]==r[y]) flag=true;
return ;
}
p[fay]=fax;
r[fay]=(r[x]+1+r[y])&1;
}
int main()
{
int T;
int cot=1;
scanf("%d",&T);
while(T--){
int n,m;
flag=false;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) p[i]=i,r[i]=0;
for(int i=0;i<m;i++){
int x,y;
scanf("%d%d",&x,&y);
if(flag) continue;
Union(x,y);
}
printf("Scenario #%d:\n",cot++);
if(flag) printf("Suspicious bugs found!\n");
else printf("No suspicious bugs found!\n");
printf("\n");
}
return 0;
}