Submission #2230391
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
int n;
set<pair<int, bool> > edges[100001];
int main() {
stack<pair<int, int> > stk;
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
int x, y;
scanf("%d%d", &x, &y), --x, --y;
edges[x].insert(make_pair(y, 0));
edges[y].insert(make_pair(x, 0));
}
for (int i = 1; i < n; ++i) {
int x, y;
scanf("%d%d", &x, &y), --x, --y;
edges[x].insert(make_pair(y, 1));
edges[y].insert(make_pair(x, 1));
stk.push(make_pair(x, y));
}
int cnt = 0;
while (!stk.empty()) {
pair<int, int> edge = stk.top();
stk.pop();
int x = edge.first, y = edge.second;
auto it = edges[x].find(make_pair(y, 0));
if (it != edges[x].end()) {
__typeof(it) it2 = it;
++it2;
if (it2 != edges[x].end() && it2->first == y) {
if (edges[x].size() < edges[y].size()) swap(x, y);
++cnt;
edges[x].erase(make_pair(y, 0));
edges[x].erase(make_pair(y, 1));
edges[y].erase(make_pair(x, 0));
edges[y].erase(make_pair(x, 1));
for (auto it3 = edges[y].begin(); it3 != edges[y].end();) {
stk.push(make_pair(x, it3->first));
edges[it3->first].erase(make_pair(y, it3->second));
edges[it3->first].insert(make_pair(x, it3->second));
edges[x].insert(*it3);
it3 = edges[y].erase(it3);
}
}
}
}
puts(cnt == n - 1 ? "YES" : "NO");
}
Submission Info
Submission Time |
|
Task |
E - Blue and Red Tree |
User |
UglyAndPretty |
Language |
C++14 (GCC 5.4.1) |
Score |
1400 |
Code Size |
1371 Byte |
Status |
AC |
Exec Time |
297 ms |
Memory |
24448 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:9:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
^
./Main.cpp:12:34: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &y), --x, --y;
^
./Main.cpp:18:34: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &y), --x, --y;
^
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
1400 / 1400 |
Status |
|
|
Set Name |
Test Cases |
Sample |
sample1.txt, sample2.txt, sample3.txt |
All |
sample1.txt, sample2.txt, sample3.txt, in1.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in2.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in29.txt, in3.txt, in30.txt, in31.txt, in32.txt, in33.txt, in34.txt, in35.txt, in36.txt, in37.txt, in38.txt, in39.txt, in4.txt, in40.txt, in41.txt, in42.txt, in43.txt, in44.txt, in45.txt, in46.txt, in47.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt, sample1.txt, sample2.txt, sample3.txt |
Case Name |
Status |
Exec Time |
Memory |
in1.txt |
AC |
3 ms |
4992 KB |
in10.txt |
AC |
121 ms |
24448 KB |
in11.txt |
AC |
210 ms |
24448 KB |
in12.txt |
AC |
211 ms |
24448 KB |
in13.txt |
AC |
211 ms |
24448 KB |
in14.txt |
AC |
212 ms |
24448 KB |
in15.txt |
AC |
208 ms |
24448 KB |
in16.txt |
AC |
210 ms |
24448 KB |
in17.txt |
AC |
212 ms |
24448 KB |
in18.txt |
AC |
231 ms |
24448 KB |
in19.txt |
AC |
242 ms |
24448 KB |
in2.txt |
AC |
3 ms |
4992 KB |
in20.txt |
AC |
220 ms |
24448 KB |
in21.txt |
AC |
224 ms |
24448 KB |
in22.txt |
AC |
217 ms |
24448 KB |
in23.txt |
AC |
217 ms |
24448 KB |
in24.txt |
AC |
217 ms |
24448 KB |
in25.txt |
AC |
217 ms |
24448 KB |
in26.txt |
AC |
215 ms |
24448 KB |
in27.txt |
AC |
217 ms |
24448 KB |
in28.txt |
AC |
212 ms |
24448 KB |
in29.txt |
AC |
215 ms |
24448 KB |
in3.txt |
AC |
3 ms |
4992 KB |
in30.txt |
AC |
218 ms |
24448 KB |
in31.txt |
AC |
291 ms |
24448 KB |
in32.txt |
AC |
287 ms |
24448 KB |
in33.txt |
AC |
282 ms |
24448 KB |
in34.txt |
AC |
284 ms |
24448 KB |
in35.txt |
AC |
294 ms |
24448 KB |
in36.txt |
AC |
284 ms |
24448 KB |
in37.txt |
AC |
284 ms |
24448 KB |
in38.txt |
AC |
271 ms |
24448 KB |
in39.txt |
AC |
265 ms |
24448 KB |
in4.txt |
AC |
125 ms |
24448 KB |
in40.txt |
AC |
275 ms |
24448 KB |
in41.txt |
AC |
286 ms |
24448 KB |
in42.txt |
AC |
285 ms |
24448 KB |
in43.txt |
AC |
287 ms |
24448 KB |
in44.txt |
AC |
297 ms |
24448 KB |
in45.txt |
AC |
285 ms |
24448 KB |
in46.txt |
AC |
270 ms |
24448 KB |
in47.txt |
AC |
269 ms |
24448 KB |
in5.txt |
AC |
122 ms |
24448 KB |
in6.txt |
AC |
273 ms |
24448 KB |
in7.txt |
AC |
124 ms |
24448 KB |
in8.txt |
AC |
124 ms |
24448 KB |
in9.txt |
AC |
132 ms |
24448 KB |
sample1.txt |
AC |
3 ms |
4992 KB |
sample2.txt |
AC |
3 ms |
4992 KB |
sample3.txt |
AC |
3 ms |
4992 KB |