fzu2192, species and search set

Source: Internet
Author: User

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

Related Article

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.