# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
61511 |
2018-07-26T06:15:04 Z |
김세빈(#1779) |
Min-max tree (BOI18_minmaxtree) |
C++11 |
|
216 ms |
15952 KB |
#include <bits/stdc++.h>
using namespace std;
struct chain
{
int s, e, v;
chain() {}
chain(int s, int e, int v) : s(s), e(e), v(v) {}
};
vector <chain> X, Y;
vector <int> V[70707], T;
int P[70707], D[70707];
int K1[70707], K2[70707], C1[70707], C2[70707];
vector <int> G[70707];
int M[70707], chk[70707];
int n, k, cnt;
void dfs(int p, int r)
{
P[p] = K1[p] = K2[p] = r;
D[p] = D[r] + 1;
for(int t: V[p]){
if(t != r) dfs(t, p);
}
}
bool find_match(int p, int c)
{
chk[p] = c;
for(int t: G[p]){
if(M[t] == -1 || (chk[M[t]] != c && find_match(M[t], c))){
M[t] = p;
return 1;
}
}
return 0;
}
int main()
{
int i, a, b, c;
char str[3];
scanf("%d", &n);
for(i=1; i<n; i++){
scanf("%d%d", &a, &b);
V[a].push_back(b);
V[b].push_back(a);
}
for(i=1; i<=n; i++){
C1[i] = C2[i] = M[i] = -1;
}
dfs(1, 0);
scanf("%d", &k);
for(i=1; i<=k; i++){
scanf("%s%d%d%d", str, &a, &b, &c);
if(*str == 'M'){
X.push_back(chain(a, b, c));
}
else{
Y.push_back(chain(a, b, c));
}
}
sort(X.begin(), X.end(), [=](chain ca, chain cb){
return ca.v < cb.v;
});
for(i=0; i<X.size(); i++){
a = X[i].s, b = X[i].e, c = X[i].v;
T.clear();
for(; a!=b; ){
if(D[a] < D[b]) swap(a, b);
if(C1[a] == -1){
C1[a] = X[i].v;
G[i].push_back(a);
}
T.push_back(a); a = K1[a];
}
for(int t: T) K1[t] = a;
}
sort(Y.begin(), Y.end(), [=](chain ca, chain cb){
return ca.v > cb.v;
});
for(i=0; i<Y.size(); i++){
a = Y[i].s, b = Y[i].e, c = Y[i].v;
T.clear();
for(; a!=b; ){
if(D[a] < D[b]) swap(a, b);
if(C2[a] == -1){
C2[a] = Y[i].v;
G[X.size() + i].push_back(a);
}
T.push_back(a); a = K2[a];
}
for(int t: T) K2[t] = a;
}
for(i=0; i<k; i++){
find_match(i, ++cnt);
}
for(i=2; i<=n; i++){
if(C2[i] != -1 && M[i] >= X.size()) printf("%d %d %d\n", i, P[i], C2[i]);
else if(C1[i] != -1) printf("%d %d %d\n", i, P[i], C1[i]);
else printf("%d %d %d\n", i, P[i], 12345);
}
return 0;
}
Compilation message
minmaxtree.cpp: In function 'int main()':
minmaxtree.cpp:77:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<X.size(); i++){
~^~~~~~~~~
minmaxtree.cpp:97:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<Y.size(); i++){
~^~~~~~~~~
minmaxtree.cpp:118:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(C2[i] != -1 && M[i] >= X.size()) printf("%d %d %d\n", i, P[i], C2[i]);
~~~~~^~~~~~~~~~~
minmaxtree.cpp:47:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
minmaxtree.cpp:50:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~
minmaxtree.cpp:61:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &k);
~~~~~^~~~~~~~~~
minmaxtree.cpp:64:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s%d%d%d", str, &a, &b, &c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
3704 KB |
Output is correct |
2 |
Correct |
6 ms |
3776 KB |
Output is correct |
3 |
Correct |
5 ms |
3776 KB |
Output is correct |
4 |
Correct |
8 ms |
3776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
206 ms |
14552 KB |
Output is correct |
2 |
Correct |
151 ms |
14552 KB |
Output is correct |
3 |
Correct |
132 ms |
14552 KB |
Output is correct |
4 |
Correct |
173 ms |
15264 KB |
Output is correct |
5 |
Correct |
171 ms |
15264 KB |
Output is correct |
6 |
Correct |
176 ms |
15264 KB |
Output is correct |
7 |
Correct |
183 ms |
15264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
129 ms |
15264 KB |
Output is correct |
2 |
Correct |
149 ms |
15264 KB |
Output is correct |
3 |
Correct |
159 ms |
15264 KB |
Output is correct |
4 |
Correct |
157 ms |
15264 KB |
Output is correct |
5 |
Correct |
147 ms |
15264 KB |
Output is correct |
6 |
Correct |
129 ms |
15264 KB |
Output is correct |
7 |
Correct |
115 ms |
15264 KB |
Output is correct |
8 |
Correct |
146 ms |
15264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
3704 KB |
Output is correct |
2 |
Correct |
6 ms |
3776 KB |
Output is correct |
3 |
Correct |
5 ms |
3776 KB |
Output is correct |
4 |
Correct |
8 ms |
3776 KB |
Output is correct |
5 |
Correct |
206 ms |
14552 KB |
Output is correct |
6 |
Correct |
151 ms |
14552 KB |
Output is correct |
7 |
Correct |
132 ms |
14552 KB |
Output is correct |
8 |
Correct |
173 ms |
15264 KB |
Output is correct |
9 |
Correct |
171 ms |
15264 KB |
Output is correct |
10 |
Correct |
176 ms |
15264 KB |
Output is correct |
11 |
Correct |
183 ms |
15264 KB |
Output is correct |
12 |
Correct |
129 ms |
15264 KB |
Output is correct |
13 |
Correct |
149 ms |
15264 KB |
Output is correct |
14 |
Correct |
159 ms |
15264 KB |
Output is correct |
15 |
Correct |
157 ms |
15264 KB |
Output is correct |
16 |
Correct |
147 ms |
15264 KB |
Output is correct |
17 |
Correct |
129 ms |
15264 KB |
Output is correct |
18 |
Correct |
115 ms |
15264 KB |
Output is correct |
19 |
Correct |
146 ms |
15264 KB |
Output is correct |
20 |
Correct |
192 ms |
15264 KB |
Output is correct |
21 |
Correct |
169 ms |
15264 KB |
Output is correct |
22 |
Correct |
124 ms |
15264 KB |
Output is correct |
23 |
Correct |
144 ms |
15264 KB |
Output is correct |
24 |
Correct |
172 ms |
15264 KB |
Output is correct |
25 |
Correct |
197 ms |
15952 KB |
Output is correct |
26 |
Correct |
216 ms |
15952 KB |
Output is correct |
27 |
Correct |
152 ms |
15952 KB |
Output is correct |
28 |
Correct |
196 ms |
15952 KB |
Output is correct |
29 |
Correct |
196 ms |
15952 KB |
Output is correct |
30 |
Correct |
160 ms |
15952 KB |
Output is correct |
31 |
Correct |
183 ms |
15952 KB |
Output is correct |
32 |
Correct |
192 ms |
15952 KB |
Output is correct |
33 |
Correct |
160 ms |
15952 KB |
Output is correct |
34 |
Correct |
185 ms |
15952 KB |
Output is correct |
35 |
Correct |
129 ms |
15952 KB |
Output is correct |