POJ 1182 food chain (type and query set), poj1182

Source: Internet
Author: User

POJ 1182 food chain (type and query set), poj1182

Address: POJ 1182

A very classic type and query set questions. Maintain the weights by using the relationships between them.

The Code is as follows:

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;int bin[60000], rank[60000];int find1(int x){    int y;    if(bin[x]!=x)    {        y=bin[x];        bin[x]=find1(bin[x]);        rank[x]=(rank[x]+rank[y])%3;    }    return bin[x];}int main(){    int n, k, p, x, y, i, flag, f1, f2, cnt=0;    scanf("%d%d",&n,&k);    for(i=1;i<=n;i++)    {        bin[i]=i;        rank[i]=0;    }    while(k--)    {        flag=0;        scanf("%d%d%d",&p,&x,&y);        if(x>n||y>n||(p==2&&x==y))        {            cnt++;            continue ;        }        f1=find1(x);        f2=find1(y);        if(p==1)        {            if(f1==f2)            {                if(rank[x]!=rank[y])                    cnt++;            }            else            {                bin[f2]=f1;                rank[f2]=(rank[x]+3-rank[y])%3;            }        }        else        {            if(f1==f2)            {                if(rank[x]!=(rank[y]+1)%3)                    cnt++;            }            else            {                bin[f2]=f1;                rank[f2]=(rank[x]+2-rank[y])%3;            }        }    }    printf("%d\n",cnt);    return 0;}



What are the uses and query sets of Peking University acm questions?

POJ 1611 The Suspects most basic query set

POJ 2524 basic Ubiquitous Religions query set
POJ 1182 extension of food chain and query set
Note: There is only one group of data;
Make full use of the conditions given by the question: there are three types of animals A, B, C, and the food chains of these three types of animals.
It constitutes an interesting ring. A eats B, B eats C, and C eats. That is to say, there are only three groups.
POJ 2492 A Bug's Life and expansion of query Sets
Method 1: depth-first Traversal
Each traversal record indicates whether the path is male or female. Only male-> female, female-> male meets the requirement. Otherwise, locate the homosexuality and end the program.
Method 2: Bipartite Graph Matching
Method 3: Expand the query set: it is very similar to 1182, but there are two groups, and 1182 is three groups, 1611 is not limited.
POJ 1861 Network = zju_1542 and query set + custom sorting + greedy search "minimal spanning tree"
The answer is not unique, but QSORT () and SORT () can be used on ZOJ, and only SORT () can be used on POJ...
POJ 1703 Find them, Catch them and query the expansion of the Set
This is very similar to POJ 2492 A Bug's Life, that is, the code will be AC after A slight modification!
Note: And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. There are only two groups.
POJ 2236 Wireless Network and query set Application
Note: 1. Check the Set; 2. The range of N can be 1001; 3. From the N + 1 line, the first input can be a string.
POJ 1988 Cube Stacking and good query set Application
1. Like Galaxy hero legend = NOI2002 Galaxy; 2. An array behind [x] is added to record the relative position of warship x in the column; 3. For detailed problem report, see Galaxy hero legend.

JOJ 1905 Freckles = POJ 2560 Minimum Spanning Tree

Method 1: Prim algorithm; Method 2: Query sets to implement the Kruskar Algorithm for the Minimum Spanning Tree


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.