bzoj4195 noi2015 day1 t1

离散化+并查集

set比unique慢好多

unique

bzoj4195 noi2015 day1 t1
 1 #include<cstdio>

 2 #include<cstring>

 3 #include<cmath>

 4 #include<ctime>

 5 #include<cstdlib>

 6 #include<iostream>

 7 #include<algorithm>

 8 #include<set>

 9 #define clr(a,x) memset(a,x,sizeof(a))

10 #define rep(i,l,r) for(int i=l;i<r;i++)

11 using namespace std;

12 int read()

13 {

14     char c=getchar();

15     int ans=0,f=1;

16     while(!isdigit(c)){

17         if(c=='-') f=-1;

18         c=getchar();

19     }

20     while(isdigit(c)){

21         ans=ans*10+c-'0';

22         c=getchar();

23     }

24     return ans*f;

25 }

26 const int maxn=200005;

27 int f[maxn],a[maxn][3],b[maxn][2],c[maxn];

28 int find(int a)

29 {

30     return (f[a]==a?f[a]:f[a]=find(f[a]));

31 }

32 int main()

33 {    

34     freopen("prog10.in","r",stdin);

35     freopen("prog10.out","w",stdout);

36     int t=read();

37     while(t--){    

38         int n=read(),cnt=0,cnt1=0;

39         bool flag=1;

40         clr(a,0),clr(b,0),clr(c,0);

41         rep(i,0,maxn) f[i]=i;

42         rep(i,0,n){

43             a[i][0]=read();

44             a[i][1]=read();

45             a[i][2]=read();

46             if(!a[i][2]){

47                 b[cnt][0]=a[i][0];

48                 b[cnt++][1]=a[i][1];

49             }

50             c[cnt1++]=a[i][0];

51              c[cnt1++]=a[i][1];

52         }

53         cnt1=unique(c,c+(n<<1))-c;

54         sort(c,c+cnt1);

55         rep(i,0,n){

56             if(a[i][2]==1){

57                 f[find(lower_bound(c,c+cnt1,a[i][0])-c)]=find(lower_bound(c,c+cnt1,a[i][1])-c);

58             }

59         }

60         rep(i,0,cnt){

61             if(find(lower_bound(c,c+cnt1,b[i][0])-c)==find(lower_bound(c,c+cnt1,b[i][1])-c)){

62                 flag=0;break;

63             }

64         }

65         if(flag) printf("YES\n");

66         else printf("NO\n");

67     }

68     return 0;

69 }
View Code

 set

bzoj4195 noi2015 day1 t1
 1 #include<cstdio>

 2 #include<cstring>

 3 #include<cmath>

 4 #include<ctime>

 5 #include<cstdlib>

 6 #include<iostream>

 7 #include<algorithm>

 8 #include<set>

 9 #define clr(a,x) memset(a,x,sizeof(a))

10 #define rep(i,l,r) for(int i=l;i<r;i++)

11 using namespace std;

12 int read()

13 {

14     char c=getchar();

15     int ans=0,f=1;

16     while(!isdigit(c)){

17         if(c=='-') f=-1;

18         c=getchar();

19     }

20     while(isdigit(c)){

21         ans=ans*10+c-'0';

22         c=getchar();

23     }

24     return ans*f;

25 }

26 set<int>S;

27 const int maxn=200005;

28 int f[maxn],a[maxn][3],b[maxn][2],c[maxn];

29 int find(int a)

30 {

31     return (f[a]==a?f[a]:f[a]=find(f[a]));

32 }

33 int main()

34 {    

35     freopen("prog10.in","r",stdin);

36     freopen("prog10.out","w",stdout);

37     int t=read();

38     while(t--){

39         S.clear();

40         int n=read(),cnt=0,cnt1=0;

41         bool flag=1;

42         clr(a,0),clr(b,0),clr(c,0);

43         rep(i,0,n<<1) f[i]=i;

44         rep(i,0,n){

45             a[i][0]=read();

46             a[i][1]=read();

47             a[i][2]=read();

48             if(!a[i][2]){

49                 b[cnt][0]=a[i][0];

50                 b[cnt++][1]=a[i][1];

51             }

52             if(S.find(a[i][0])==S.end()){

53                 S.insert(a[i][0]);

54                 c[cnt1++]=a[i][0];

55             }

56             if(S.find(a[i][1])==S.end()){    

57                 S.insert(a[i][1]);

58                 c[cnt1++]=a[i][1];

59             }

60         }

61         sort(c,c+cnt1);

62         rep(i,0,n){

63             if(a[i][2]==1){

64                 f[find(lower_bound(c,c+cnt1,a[i][0])-c)]=find(lower_bound(c,c+cnt1,a[i][1])-c);

65             }

66         }

67         rep(i,0,cnt){

68             if(find(lower_bound(c,c+cnt1,b[i][0])-c)==find(lower_bound(c,c+cnt1,b[i][1])-c)){

69                 flag=0;break;

70             }

71         }

72         if(flag) printf("YES\n");

73         else printf("NO\n");

74     }

75     return 0;

76 }
View Code

 

你可能感兴趣的:(ZOJ)