bzoj4668 Cold War

Source: Internet
Author: User

Cold War time Limit:10 Sec Memory limit:256 mbdescription

March 5, 1946, former British Prime minister Winston Churchill in the United States, Fulton published "Iron
The prelude to the Cold War.
The United States and the Soviet Union are the world's "superpowers", in order to compete for world hegemony, the two countries and their
The allies have waged decades of struggle. During this period, although differences and conflicts were serious, both sides
Efforts to avoid the outbreak of world-wide wars (Third World War), their confrontation usually
War, technology and arms race, space competition, diplomatic competition and other "cold" way into
"Mutual containment, non-force", so called the "Cold War".
Reddington is the admiral of the United States. Because of the tense situation in the war, he needed
Keep an eye on the activities of the Soviet Union and avoid getting its own country into trouble. The Soviet Union has a global
There were N military plants, but due to improper planning, there was no railroad between these military plants at first
, in order to make weapons faster, the Soviet Union decided to build several roads that would allow some military factories to connect.
Reddington got a schedule of Soviet construction, and he needed to keep an eye on some two military
If the factory is connected, and which road will be built at the earliest. Specifically, there are now a total of
M operations, the operation is divided into two categories:
? 0 U V, this operation the Soviet Union will build an iron that connects the U-Military plant and the V-plant.
Road, pay attention to the railway are two-way;
? 1 U V, Reddington needs to know that the U military plant and the V plant were first added to the
After a few railways will be unicom, if this operation is not connected, then output 0;
As America's strongest scientist, Reddington needs your help designing a program that can meet
His request.

Input

First line two integers N, M.
Next M line, each behavior 0 u v or 1 u v form.
The data is encrypted, for each edge or query, the true U, v equals the read-in
U, v XOR or the answer to the previous query. At first this value is 0.
1≤n, m≤500000, decrypted u, v satisfies 1≤u, v≤n, U is not equal to V

Output

For each 1 operation, the output U, v the first to join the side will be unicom, if
When the work is not connected, the output 0.

Sample Input

5 9

0 1 4

1 2 5

0 2 4

0 3 4

1 3 1

0 7 0

0 6 1

0 1 6

1 2 6

Sample Output

0

3




And the search set heuristic merger is the scientific violence qaq ...
Because there's information on your path, you can't compress the path.
And then your find function is going to be written as recursion, and every time I find it, Dad updates the message. Qaq

#include <bits/stdc++.h>using namespace Std;const int maxn = 5e5 + 5;int N, m, tot, Lastans;int mx, FA[MAXN], Tim[max    N], Deep[maxn], len[maxn];int find (int t) {if (t = = Fa[t]) {deep[t] = 1; return t;}    int ret = find (fa[t]); DEEP[T] = deep[fa[t]] + 1; return ret;}    inline void Connect (int a, int b) {int a = find (a), B = Find (b); tot++;    if (A = = B) return;    if (Len[a] > Len[b]) swap (A, B); Fa[a] = B;    Tim[a] = tot; LEN[B] = max (Len[b], Len[a] + 1);}    inline int Query (int a, int b) {int a = find (a), B = Find (b);    if (A! = B) return 0;    if (Deep[a] < deep[b]) swap (A, b);    int ret = 0;    while (Deep[a] > Deep[b]) {ret = max (ret, tim[a]); a = Fa[a];}        while (A! = b) {ret = max (ret, tim[a]); ret = max (ret, tim[b]); A = Fa[a];    b = Fa[b]; } return ret;}    int main () {//freopen ("lpl.in", "R", stdin);    scanf ("%d%d", &n, &m);    for (int i = 1; I <= n; ++i) {fa[i] = i; len[i] = 1;}    int opt, u, v; while (m--) {scanf ("%d%d%d ", &opt, &u, &v); U ^= Lastans;        V ^= Lastans;        if (!opt) Connect (U, v);    Else{lastans = Query (U, v); printf ("%d\n", Lastans);} } return 0;}

bzoj4668 Cold War

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.