Test instructions is Chinese, I don't say it;
Here to use the species and check set, respectively, with 1-n; N-2N,2N-3N represents city, service, person;
Then pay attention to these points:
1: Do not use rank array, because even the edge to control their own.
2: In the Unite function, the comparison of X and Y, control the large connection to the top of the small, so after ruminate, find () will find the smallest (city), and then if greater than n, it means that he did not connect to the city, output 0;
The code is as follows:
#include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include < Math.h> #include <stdlib.h>using namespace std;const int n = 300000;int par[999999], last[999999];void init (int n ) {int I, j;for (int i = 0; i<n; i++) {Par[i] = i;}} int find (int x) {if (par[x]! = x) par[x] = find (par[x]); return par[x];} void unite (int x, int y) {x = find (x), y = Find (Y), if (x = = y) return;int Mmax = max (x, y), int mmin = min (x, y);p Ar[mmax] =m min;} bool Same (int x, int y) {return find (x) = = Find (y);} int main () {int n, m, Q, I, J, K, Good, city, people, kind, query;while (~scanf ("%d%d%d", &n, &m, &q)) {init (999 999); for (i = 0; i<n; i++) {scanf ("%d%d", &good, &city), if (city) {Unite (good + N, city);}} for (i = 0; i<m; i++) {scanf ("%d%d%d", &people, &good, &city), if (city) {Unite (people + 2 * n,good+n); Unite (g Ood + N, city);} Else{unite (People + 2 * N, good + N);}} for (i = 0; i<q; i++) {int temp;scanf ("%d%d", &kind, &query); IF (Kind = = 0) {temp = find (query + N);} if (kind = = 1) {temp = find (query + 2*n);} if (temp>n) {printf ("0\n"); continue;} printf ("%d\n", temp);}} return 0;}
fzu2192, species and search set