forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathG.cpp
More file actions
127 lines (108 loc) · 2.83 KB
/
G.cpp
File metadata and controls
127 lines (108 loc) · 2.83 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define SZ(x) ((int) (x).size())
using namespace std;
struct Node {
int x;
Node* next;
};
Node* createNode(int _x) {
Node* res = new Node();
res->x = _x;
res->next = 0;
return res;
}
struct List {
Node* head, *tail;
List() { head = tail = 0; }
void print() {
Node* p = head;
while (p) {
cout << p->x << ' ';
p = p->next;
}
cout << endl;
}
};
const int MN = 100111;
int lab[MN];
List cur[MN];
int n, nHor, q;
struct Hor {
int y;
vector<int> x;
friend istream& operator >> (istream& cin, Hor& a) {
cin >> a.y;
int l; cin >> l;
a.x.resize(l);
REP(i,l) cin >> a.x[i];
return cin;
}
} hors[MN];
bool operator < (const Hor& a, const Hor& b) {
return a.y > b.y;
}
int getRoot(int u) {
if (lab[u] < 0) return u;
return lab[u] = getRoot(lab[u]);
}
void merge(int u, int v) {
u = getRoot(u); v = getRoot(v);
if (u == v) return ;
if (u > v) swap(u, v);
// u < v
lab[u] += lab[v];
lab[v] = u;
}
int res[MN];
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
freopen("g.in", "r", stdin);
freopen("g.out", "w", stdout);
while (cin >> n >> nHor >> q) {
FOR(i,1,nHor) cin >> hors[i];
sort(hors+1, hors+nHor+1);
FOR(i,1,n) {
lab[i] = -1;
Node* p = createNode(i);
cur[i].head = cur[i].tail = p;
}
FOR(i,1,nHor) {
// DEBUG(hors[i].y);
vector<int> x;
for(int u : hors[i].x) {
x.push_back(getRoot(u));
}
sort(x.begin(), x.end());
int u0 = x[0];
FOR(i,1,SZ(x)-1) {
int u = x[i];
merge(u, u0);
cur[u0].tail->next = cur[u].head;
cur[u0].tail = cur[u].tail;
}
// PR0(x, SZ(x));
// cur[u0].print();
}
int t = 0;
FOR(i,1,n) if (lab[i] < 0) {
Node* p = cur[i].head;
while (p) {
res[++t] = p->x;
p = p->next;
}
}
while (q--) {
int id; cin >> id;
cout << res[id] << '\n';
}
cout << '\n';
}
}