generated from TNFSH-Programming-Contest/tps-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqnlgn.cpp
70 lines (61 loc) · 1.3 KB
/
qnlgn.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "testlib.h"
#include <bits/stdc++.h>
#define rep(i,j,k) for (int i=j; i<=k; i++)
#define pb push_back
#define ff first
#define ss second
#define de(x) cout << #x << '=' << x << ", "
#define dd cout << '\n';
#define lyx ios::sync_with_stdio(0), cin.tie(0);
using namespace std;
typedef pair<int,int> pii;
const int N = 1000, Q = 1000;
int n, q;
vector<vector<int>> G(N);
struct DSU
{
int p[N+1];
void init()
{
rep(i,1,n) p[i] = i;
}
int find(int i)
{
return (p[i]==i? i:p[i]=find(p[i]));
}
void U(int u, int v)
{
p[find(u)] = find(v);
}
} dsu;
int main() {
registerValidation();
n = inf.readInt(1,N);
inf.readSpace();
q = inf.readInt(1,Q);
inf.readEoln();
dsu.init();
rep(i,2,n)
{
int u = inf.readInt(1,n);
inf.readSpace();
int v = inf.readInt(1,n);
inf.readEoln();
G[u].pb(v);
G[v].pb(u);
ensuref(u!=v, "u should no equal to v");
ensuref(dsu.find(u) != dsu.find(v), "cycle shoudn't exist");
dsu.U(u,v);
}
rep(i,1,q)
{
int x = inf.readInt(1,n);
inf.readSpace();
int y = inf.readInt(1,n);
inf.readSpace();
int z = inf.readInt(1,n);
inf.readEoln();
}
inf.readEof();
return 0;
}