Submission #1610097
Source Code Expand
#include <bits/stdc++.h>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define mset(x, y) memset(x, y, sizeof x)
#define mcpy(x, y) memcpy(x, y, sizeof x)
using namespace std;
typedef long long LL;
typedef pair <int, int> pii;
inline int Read()
{
int x = 0, f = 1, c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-')
f = -1;
for (; isdigit(c); c = getchar())
x = x * 10 + c - '0';
return x * f;
}
const int MAXN = 100005;
const int INF = 0x3f3f3f3f;
int n, tim, dfn[MAXN], dep[MAXN], par[MAXN], bel[MAXN], siz[MAXN];
set <pii> S[MAXN << 2];
vector <int> adj[MAXN];
pii mn[MAXN << 2];
inline void Dfs(int x)
{
siz[x] = 1;
for (auto y : adj[x])
if (y ^ par[x])
par[y] = x, dep[y] = dep[x] + 1, Dfs(y), siz[x] += siz[y];
}
inline void Dfs(int x, int c)
{
dfn[x] = ++ tim, bel[x] = c;
int k = 0;
for (auto y : adj[x])
if (y ^ par[x])
if (siz[y] > siz[k])
k = y;
if (!k)
return ;
Dfs(k, c);
for (auto y : adj[x])
if (y ^ par[x])
if (y ^ k)
Dfs(y, y);
}
inline void Pushup(int x)
{
mn[x] = min(mn[x << 1], mn[x << 1 | 1]);
mn[x].xx += S[x].size();
}
inline void Build(int x, int l, int r)
{
mn[x].yy = l;
if (l == r)
return ;
int mid = l + r >> 1;
Build(x << 1, l, mid), Build(x << 1 | 1, mid + 1, r);
}
inline void Insert(int x, int l, int r, int ql, int qr, pii v)
{
if (l == ql && r == qr)
return (void)(S[x].insert(v), mn[x].xx ++);
int mid = l + r >> 1;
if (qr <= mid)
Insert(x << 1, l, mid, ql, qr, v);
else if (ql > mid)
Insert(x << 1 | 1, mid + 1, r, ql, qr, v);
else
Insert(x << 1, l, mid, ql, mid, v), Insert(x << 1 | 1, mid + 1, r, mid + 1, qr, v);
Pushup(x);
}
inline void Add(int x, int y)
{
int tx = x, ty = y;
while (bel[tx] ^ bel[ty])
{
if (dep[bel[tx]] < dep[bel[ty]])
swap(tx, ty);
Insert(1, 2, n, dfn[bel[tx]], dfn[tx], mp(x, y));
tx = par[bel[tx]];
}
if (dfn[tx] > dfn[ty])
swap(tx, ty);
if (tx ^ ty)
Insert(1, 2, n, dfn[tx] + 1, dfn[ty], mp(x, y));
}
inline void Erase(int x, int l, int r, int ql, int qr, pii v)
{
if (l == ql && r == qr)
return (void)(S[x].erase(v), mn[x].xx --);
int mid = l + r >> 1;
if (qr <= mid)
Erase(x << 1, l, mid, ql, qr, v);
else if (ql > mid)
Erase(x << 1 | 1, mid + 1, r, ql, qr, v);
else
Erase(x << 1, l, mid, ql, mid, v), Erase(x << 1 | 1, mid + 1, r, mid + 1, qr, v);
Pushup(x);
}
inline void Del(int x, int y)
{
int tx = x, ty = y;
while (bel[tx] ^ bel[ty])
{
if (dep[bel[tx]] < dep[bel[ty]])
swap(tx, ty);
Erase(1, 2, n, dfn[bel[tx]], dfn[tx], mp(x, y));
tx = par[bel[tx]];
}
if (dfn[tx] > dfn[ty])
swap(tx, ty);
if (tx ^ ty)
Erase(1, 2, n, dfn[tx] + 1, dfn[ty], mp(x, y));
}
inline void GG()
{
puts("NO");
exit(0);
}
inline pii Query(int x, int l, int r, int p)
{
if (S[x].size())
return *S[x].begin();
int mid = l + r >> 1;
if (p <= mid)
return Query(x << 1, l, mid, p);
return Query(x << 1 | 1, mid + 1, r, p);
}
inline void Modify(int x, int l, int r, int p)
{
if (l == r)
return (void)(mn[x].xx = INF);
int mid = l + r >> 1;
if (p <= mid)
Modify(x << 1, l, mid, p);
else
Modify(x << 1 | 1, mid + 1, r, p);
Pushup(x);
}
int main()
{
#ifdef wxh010910
freopen("data.in", "r", stdin);
#endif
n = Read();
for (int i = 1, x, y; i < n; i ++)
x = Read(), y = Read(), adj[x].pb(y), adj[y].pb(x);
Dfs(1), Dfs(1, 1), Build(1, 2, n);
for (int i = 1, x, y; i < n; i ++)
Add(Read(), Read());
for (int i = 1; i < n; i ++)
{
if (mn[1].xx ^ 1)
GG();
int p = mn[1].yy;
pii t = Query(1, 2, n, p);
Del(t.xx, t.yy), Modify(1, 2, n, p);
}
return puts("YES"), 0;
}
Submission Info
Submission Time |
|
Task |
E - Blue and Red Tree |
User |
wxh010910 |
Language |
C++14 (GCC 5.4.1) |
Score |
1400 |
Code Size |
3803 Byte |
Status |
AC |
Exec Time |
1409 ms |
Memory |
124416 KB |
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 |
9 ms |
22784 KB |
in10.txt |
AC |
1340 ms |
94976 KB |
in11.txt |
AC |
350 ms |
48000 KB |
in12.txt |
AC |
349 ms |
47232 KB |
in13.txt |
AC |
341 ms |
47488 KB |
in14.txt |
AC |
345 ms |
47488 KB |
in15.txt |
AC |
341 ms |
47744 KB |
in16.txt |
AC |
339 ms |
47232 KB |
in17.txt |
AC |
349 ms |
47744 KB |
in18.txt |
AC |
353 ms |
47744 KB |
in19.txt |
AC |
345 ms |
47488 KB |
in2.txt |
AC |
9 ms |
22784 KB |
in20.txt |
AC |
361 ms |
47744 KB |
in21.txt |
AC |
410 ms |
51712 KB |
in22.txt |
AC |
398 ms |
51328 KB |
in23.txt |
AC |
430 ms |
52224 KB |
in24.txt |
AC |
429 ms |
52480 KB |
in25.txt |
AC |
425 ms |
52608 KB |
in26.txt |
AC |
343 ms |
47104 KB |
in27.txt |
AC |
367 ms |
47872 KB |
in28.txt |
AC |
365 ms |
47744 KB |
in29.txt |
AC |
362 ms |
48128 KB |
in3.txt |
AC |
9 ms |
22784 KB |
in30.txt |
AC |
378 ms |
47744 KB |
in31.txt |
AC |
1323 ms |
92672 KB |
in32.txt |
AC |
1394 ms |
93184 KB |
in33.txt |
AC |
1409 ms |
94464 KB |
in34.txt |
AC |
1305 ms |
92032 KB |
in35.txt |
AC |
1237 ms |
89600 KB |
in36.txt |
AC |
1255 ms |
92800 KB |
in37.txt |
AC |
1254 ms |
92672 KB |
in38.txt |
AC |
994 ms |
123008 KB |
in39.txt |
AC |
767 ms |
121344 KB |
in4.txt |
AC |
1273 ms |
93440 KB |
in40.txt |
AC |
848 ms |
119808 KB |
in41.txt |
AC |
1257 ms |
91776 KB |
in42.txt |
AC |
1244 ms |
93824 KB |
in43.txt |
AC |
1233 ms |
91776 KB |
in44.txt |
AC |
1242 ms |
90624 KB |
in45.txt |
AC |
1208 ms |
92288 KB |
in46.txt |
AC |
590 ms |
121984 KB |
in47.txt |
AC |
706 ms |
124416 KB |
in5.txt |
AC |
1318 ms |
94848 KB |
in6.txt |
AC |
761 ms |
123008 KB |
in7.txt |
AC |
1354 ms |
96768 KB |
in8.txt |
AC |
1301 ms |
95232 KB |
in9.txt |
AC |
1362 ms |
96384 KB |
sample1.txt |
AC |
9 ms |
22784 KB |
sample2.txt |
AC |
9 ms |
22784 KB |
sample3.txt |
AC |
9 ms |
22784 KB |