zoukankan      html  css  js  c++  java
  • P2024 食物链

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    int n, m, order, a, b, ans, fa[100005], re[100005];
    int find(int x) 
    {
    	int f = fa[x];
    	if(x != f) {
    		fa[x] = find(f);
    		re[x] = (re[x] + re[f]) % 3;
    		return fa[x];
    	}
    	else return f;
    } 
    int main()
    {
    	cin >> n >> m;
    	for(int i = 1; i <= n; i++)
    		fa[i] = i;
    	for(int i = 1; i <= m; i++) {
    		cin >> order >> a >> b;
    		if(a > n || b > n || (a == b && order == 2)) {
    			ans++;
    			continue;
    		}	
    		int f1 = find(a), f2 = find(b);
    		if(order == 1) {
    			if(f1 == f2 && re[a] != re[b]) {
    				ans++;
    				continue;
    			} /*else if(f1 == f2 && re[a] % 3 == re[b] % 3) {
    				continue;
    			}*/ else if(f1 != f2) {
    				fa[f1] = f2;
    				re[f1] = (re[b] - re[a] + 3) % 3;
    				continue;
    			} 
    		} else if(order == 2) {
    			if(f1 == f2 && (re[a] - re[b] + 3) % 3 != 1) {
    				ans++;
    				continue;
    			} else if(f1 != f2) {
    				fa[f1] = f2;
    				re[f1] = (re[b] + 1 - re[a] + 3) % 3;
    				continue;
    			}
    		}
    	}
    	cout << ans;
    return 0;
    }
    
  • 相关阅读:
    ASP.NET MVC 学习之路-2
    ASP.NET MVC 学习之路-1
    mvp框架
    Linq基础
    C# Lambda表达式
    三层架构基础
    Protobuf-net基础
    AutoResetEvent和ManualResetEvent
    解题报告——POJ 2299
    解题报告——POJ 2726
  • 原文地址:https://www.cnblogs.com/xuanfly/p/11851659.html
Copyright © 2011-2022 走看看