LOJ #109. query the set,

Source: Internet
Author: User

LOJ #109. query the set,
Memory limit: 256 MiB time limit: 2000 ms standard input/output question type: traditional evaluation method: text comparison uploaded by: Anonymous Submission submission record statistics discussion 1 Test Data question description

This is a template question.

Maintains an undirected graph of an nnn point. supported:

  • Add an undirected edge connecting uuu and vvv
  • Query the connectivity between uuu and vvv

Because the data in this question is large, a special output method is used for output: 000 or 111 represents the answer to each query, and the answers to each query are arranged from left to right at a time, treat the resulting string as a binary number and output the binary number mod 998244353 \ text {mod }~ The value of 992134366mod 998244353.

Input Format

The first line contains two integers n, mn, mn, m, indicating the number of vertices and the number of operations.

Next, the mmm row contains three integers, op, u, v \ text {op}, u, vop, u, and v.

  • If op = 0 \ text {op} = 0op = 0, a undirected edge connecting uuu and vvv is added;
  • If op = 1 \ text {op} = 1op = 1, the connectivity between uuu and vvv is queried.
Output Format

A row contains an integer to indicate the answer.

Sample Input
3 61 1 00 0 11 0 11 1 20 2 11 2 1
Sample output
5
Example

The answer string is 101101101.

Data range and prompt

N ≤ 4000000, m ≤ 8000000n \ le 4000000, m \ le 8000000n ≤ 4000000, m ≤ 8000000

Show category labels

 

It seems like you have seen a ghost in the past few days ..

The Rotary jamming case written yesterday is slower than the violent one,

The heuristic merge statement written today is slower than the violent merge statement ,,

 

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 using namespace std; 6 const int MAXN = 8000001; 7 const int mod = 998244353; 8 inline void read (int & n) 9 {10 char c = '+'; bool flag = 0; n = 0; 11 while (c <'0' | c> '9') c = '-'? Flag = 1, c = getchar (): c = getchar (); 12 while (c> = '0' & c <= '9 ') n = n * 10 + c-48, c = getchar (); 13} 14 int fa [MAXN]; 15 int size [MAXN]; 16 int n, m; 17 string p; 18 int find (int x) 19 {return fa [x] = x? Fa [x]: fa [x] = find (fa [x]);} 20 int query (int x, int y) 21 {return find (x) = find (y);} 22 void unionn (int x, int y) 23 {24 int fx = find (x); int fy = find (y ); 25 if (fx! = Fy) 26 {27 if (size [fx]> size [fy]) swap (fx, fy); 28 fa [fx] = fy; size [fy] + = size [fx]; 29 // fa [fx] = fy; 30} 31} 32 int ans = 0; 33 int main () 34 {35 // freopen (". in "," r ", stdin); 36 // freopen (". out "," w ", stdout); 37 read (n); read (m); 38 for (int I = 1; I <= n; I ++) fa [I] = I; 39 for (int I = 1; I <= m; I ++) 40 {41 int how; read (how); 42 if (how) // query 43 {44 int x, y; read (x); read (y); 45 ans = (ans * 2 + query (x, y) % mod; 46} 47 else // edge 48 {49 int x, y; read (x); read (y); 50 unionn (x, y ); 51} 52} 53 printf ("% d", ans); 54 return 0; 55}

 

 

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.