记录编号 205610 评测结果 AAAAAAAAAA
题目名称 [NOI 2015]程序自动分析 最终得分 100
用户昵称 Gravatar一個人的雨 是否通过 通过
代码语言 C++ 运行时间 3.220 s
提交时间 2015-11-05 16:37:23 内存使用 11.76 MiB
显示代码纯文本
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<map>
  5. #include<algorithm>
  6. using namespace std;
  7. const int maxn=500000+10;
  8. map<int,int>mp;
  9. int a[maxn],b[maxn],p[maxn];
  10. int proa[maxn],prob[maxn];
  11. int tot=0,cnt=0,n,f[maxn];
  12. char ch;
  13.  
  14. int read(){
  15. int num=0;ch=getchar();
  16. while (ch<'!') ch=getchar();
  17. while (ch>='0'&&ch<='9'){num=num*10+ch-'0';ch=getchar();}
  18. return num;
  19. }
  20.  
  21. int find(int x){
  22. return x==f[x]?x:f[x]=find(f[x]);
  23. }
  24.  
  25. int main(){
  26. freopen("prog.in","r",stdin);
  27. freopen("prog.out","w",stdout);
  28. int t; t=read();
  29. while (t--){
  30. n=read(); tot=0;
  31. mp.clear(); cnt=0;
  32. for (int i=1;i<=n;++i){
  33. a[i]=read(); b[i]=read(); p[i]=read();
  34. if (!mp[a[i]]) mp[a[i]]=++tot;
  35. if (!mp[b[i]]) mp[b[i]]=++tot;
  36. }
  37. for (int i=1;i<=tot;++i) f[i]=i;
  38. for (int i=1;i<=n;++i){
  39. int u=mp[a[i]];
  40. int v=mp[b[i]];
  41. if (p[i]==1){
  42. int p=find(u);
  43. int q=find(v);
  44. if (p!=q) f[p]=q;
  45. }else {
  46. proa[++cnt]=u;
  47. prob[cnt]=v;
  48. }
  49. }
  50. bool flag=1;
  51. for (int i=1;i<=cnt;++i){
  52. int p=find(proa[i]);
  53. int q=find(prob[i]);
  54. if (p==q){
  55. printf("NO\n");
  56. flag=0; break;
  57. }
  58. }
  59. if (flag) printf("YES\n");
  60. }
  61. //system("pause");
  62. }