1182解题报告

AccecptTime:          2008-12-17 16:45:59
Language:                 C++
Memory:                  596K
Time:                        250MS
Errors:                      6 WA
Algorithm:                并查集 + 矢量变换

 

 

  1. #include <stdio.h>
  2. int parent[50005];
  3. int relation[50005];
  4. // 对路径进行压缩的同时把关系数找出
  5. int FindParent(int x,int &rx)
  6. {
  7.     if( parent[x]) {
  8.         int xParent = FindParent(parent[x],relation[parent[x]]);
  9.         rx = ( relation[parent[x]] + rx) % 3;
  10.         return parent[x] = xParent;
  11.     }
  12.     return x;
  13. }
  14. // 注意两者对relation运算之间的差别
  15. void MergeSet(int r,int x,int y)
  16. {
  17.     if( x > y) {
  18.         parent[y] = x;
  19.         relation[y] = (-r + 3)% 3;
  20.     }
  21.     else {
  22.         parent[x] = y;
  23.         relation[x] = (r + 3)% 3;
  24.     }
  25. }
  26. int main()
  27. {
  28.     int n, d, k;
  29.     int x, y, t1, t2;
  30.     int lies = 0;
  31.     scanf("%d%d",&n,&k);
  32.     forint i = 0; i < k; i++) {
  33.         scanf("%d%d%d",&d,&x,&y);
  34.         if( x > n || y > n) {
  35.             lies++;
  36.             continue;
  37.         }
  38.         t1 = FindParent(x,relation[x]);
  39.         t2 = FindParent(y,relation[y]);
  40.         // 两个都在同一子集里
  41.         if( t1 == t2) {
  42.             if( (relation[x] - relation[y] + 3) % 3 != ( d - 1 ) )
  43.                 lies++;
  44.             continue;
  45.         }
  46.         else
  47.             MergeSet(- relation[x] + relation[y] + d - 1,t1,t2);
  48.     }
  49.     printf("%d/n",lies);
  50. }

poj上某牛说:总结此题可以精简到一个并查集!并且用向量的思考模式想整个过程相当简单!我看了他的解释并经过6次wa后终于ac了。这道题的教训是:不要被自己的第一想法给禁锢,open your mind!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值