码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1182 食物链

时间:2015-05-14 23:35:32      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

题意:就是……食物链嘛orz

 

解法:并查集+向量偏移。一!定!不!要!多!组!输!入!!!

 

代码:

难看死了

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
int father[50005], delta[50005];
void init()
{
    for(int i = 0; i < 50005; i++)
        father[i] = i;
    memset(delta, 0, sizeof delta);
}
int Find(int a)
{
    if(father[a] != a)
    {
        int tmp = Find(father[a]);
        delta[a] = (delta[a] + delta[father[a]]) % 3;
        father[a] = tmp;
    }
    return father[a];
}
int main()
{
    int n, k;
    scanf("%d%d", &n, &k);
        init();
        int ans = 0;
        for(int i = 0; i < k; i++)
        {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            if(b > n || c > n)
            {
                ans++;
                continue;
            }
            int d, e;
            d = Find(b), e = Find(c);
            if(a == 1)
            {
                if(d == e)
                {
                    //cout << "case1: " << delta[b] << " " << delta[c] << endl;
                    if(delta[b] != delta[c])
                    {
                        ans++;
                        //cout << "wrong: " << a << " " << b << " " << c << endl;
                    }
                }
                else
                {
                    father[d] = e;
                    delta[d] = (delta[c] - delta[b] + 3) % 3;
                }
            }
            else
            {
                if(d == e)
                {
                    if(((delta[b] - delta[c] + 3) % 3) != 1)
                    {
                        ans++;
                        //cout << "wrong: " << a << " " << b << " " << c << endl;
                    }
                }
                else
                {
                    father[d] = e;
                    delta[d] = (delta[c] - delta[b] + 4) % 3;
                }
            }
        }
        printf("%d\n", ans);
    return 0;
}

  

POJ 1182 食物链

标签:

原文地址:http://www.cnblogs.com/Apro/p/4504564.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!