Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

思考一下午+看题解...

Posted by 834208094 at 2013-06-08 01:38:24 on Problem 1182
其实就是并查集,但是有点变化,就是把所有的食物链上的东西都放到同一个集合里去,但是彼此之间的吃与被吃的关系,通过一个“相对位置”,再次进行判断...
#include <iostream>
#include<string>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
int dic1[1000010], dic2[1000010], dic3[1000010];
struct food
{
    int pre, relation;
}f[50010];
int getparent(int k)
{
    if(k == f[k].pre) return k;
    int mid = f[k].pre;
    f[k].pre = getparent(mid);
    f[k].relation = (f[k].relation + f[mid].relation) % 3;
    return f[k].pre;
}
int main()
{
    freopen("D:\\CPPProgram\\ACM\\in.txt", "r", stdin);
    int N, k;
    scanf("%d %d", &N, &k);
    for(int i=1; i<=N; i++)
    {
        f[i].pre = i;
        f[i].relation = 0;
    }
    int ans = 0;
    for(int i=1; i<=k; i++)
    {
        scanf("%d %d %d", &dic1[i], &dic2[i], &dic3[i]);
        if(dic2[i]>N || dic3[i] > N)
        {
            ans++;
            continue;
        }
        if(dic1[i] == 2 && dic2[i] == dic3[i])
        {
            ans++;
            continue;
        }
        int r1, r2;
        r1 = getparent(dic2[i]);
        r2 = getparent(dic3[i]);
        if(r1 != r2)
        {
            f[r2].pre = r1;
            f[r2].relation = (3 + dic1[i]-1+f[dic2[i]].relation - f[dic3[i]].relation)%3;
        }
        else
        {
            if(dic1[i] == 1 && f[dic2[i]].relation != f[dic3[i]].relation)
            {
                ans++;
                continue;
            }
            if(dic1[i] == 2 && (f[dic2[i]].relation - f[dic3[i]].relation + 30) % 3 != 2 )
            {
                ans++;
                continue;
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator