食物链
来源:互联网 发布:ubuntu17网络配置 编辑:程序博客网 时间:2024/04/20 12:22
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=34236#problem/A
1.如果在同一棵树中find(x) == find(y):直接判断是否说谎。
1)如果 d ==1,那么 x 与 y 应该是同类,他们的r[]应该相等
如果不相等,则说谎数 +1
2)如果 d==2,那么 x 应该吃了 y,也就是 (r[x]+1)%3 == r[y]
如果不满足,则说谎数 +1
如何判断 x 吃了 y 是 (r[x]+1)%3 == r[y],请看下图:(PS:箭头方向指向被吃方)
2.如果不在同一棵树中:那么合并 x 与 y 分别所在的树。
合并树时要注意顺序,我这里是把 x 树的根当做主根,否则会
WA的很惨
注意:找父亲节点时,要不断更新 r[]的值。
这里有一个关系:如果 x 和y 为关系 r1, y 和 z 为关系 r2
那么 x 和z的关系就是 (r1+r2)%3
如何证明?
无非是3*3 = 9种情况而已
(a, b) 0:同类 、 1:a被b吃 、 2:a吃b
关于合并时r[]值的更新:
如果 d == 1则 x和y 是同类 ,那么 y 对 x 的关系是 0
如果 d == 2 则 x 吃了 y, 那么 y 对 x 的关系是 1, x 对 y 的关系是 2.
综上所述 ,无论 d为1 或者是为 2, y 对 x 的关系都是 d-1
定义 :fx 为 x 的根点, fy 为 y 的根节点
合并时,如果把 y 树合并到 x 树中
如何求 fy 对 fx 的r[]关系?
fy 对 y 的关系为 3-r[y]
y 对 x 的关系为 d-1
x 对 fx 的关系为 r[x]
所以 fy 对 fx 的关系是(3-r[y] + d-1 + r[x])%3
解析转自http://blog.csdn.net/freezhanacmore/article/details/8767413
// a.cpp : ??????????????//// Author: bo_jwolf//#include "stdafx.h"#include<vector>#include<list>#include<map>#include<set>#include<deque>#include<stack>#include<bitset>#include<algorithm>#include<functional>#include<numeric>#include<utility>#include<sstream>#include<iostream>#include<iomanip>#include<cmath>#include<cstdlib>#include<cstring>#include<ctime>#include<queue>#include<stdio.h>using namespace std;const int maxn = 50005;int fa[ maxn ], re[ maxn ];void Start(){for( int i = 0; i < maxn; ++i ){fa[ i ] = i;re[ i ] = 0;}}int find(int x) { if(x == fa[x]) return x; int t = fa[x]; fa[x] = find(fa[x]); re[x] = (re[x]+re[t])%3; return fa[x];}/*int find( int x ){if( x == fa[ x ] )return x;fa[ x ] = find( x );re[ x ] = ( re[ x ] + re[ fa[ x ] ])% 3;return fa[ x ];}*/void Union( int a, int b, int d ){int x = find( a );int y = find( b );fa[ y ] = x;re[ y ] = ( re[ a ] - re[ b ] + 3 + d - 1 ) % 3;}//int _tmain(int argc, _TCHAR* argv[])int main(){int n, m, x, y, a, b, d, ans;//cin >> n >> m;scanf( "%d%d", &n, &m );Start();ans = 0;while( m-- ){scanf( "%d%d%d", &d, &a, &b );x = find( a );y = find( b );if( a > n || b > n || ( d == 2 && a == b ) )ans++;else if( x == y ){if( d == 1 && re[ a ] != re[ b ] )ans++;if( d == 2 && ( re[ a ] + 1 ) % 3 != re[ b ] )ans++;}else{Union( a, b, d );}}printf( "%d\n", ans );//system("pause");return 0;}