Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
|---|---|---|---|---|---|---|
1149_E. Election Promises
|
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes.
There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles — it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax.
Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles.
The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves?
Input
The first line of the input contains two integers n, m (1 ≤ n ≤ 200 000, 0 ≤ m ≤ 200 000) — the number of cities and roads in Byteland.
The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≤ h_i ≤ 10^9); h_i denotes the amount of taxes paid in the i-th city.
Each of the following m lines contains two integers (1 ≤ u, v ≤ n, u ≠ v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities.
We can show that the conventions cannot be held indefinitely for any correct test case.
Output
If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≤ h'_i ≤ 2 ⋅ 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 ⋅ 10^{18} bourles.
If the party cannot assure their victory, output LOSE in the first and only line of the output.
Examples
Input
4 2
2 1 1 5
1 2
3 4
Output
WIN
1 5 1 5
Input
4 2
1 5 1 5
1 2
3 4
Output
LOSE
Input
3 3
314 159 265
1 2
1 3
3 2
Output
WIN
0 0 0
Input
6 4
2 2 5 5 6 6
1 3
2 4
3 5
4 6
Output
LOSE
Note
In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally.
The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win.
In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
|
{
"input": [
"6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n",
"3 3\n314 159 265\n1 2\n1 3\n3 2\n",
"4 2\n2 1 1 5\n1 2\n3 4\n",
"4 2\n1 5 1 5\n1 2\n3 4\n"
],
"output": [
"LOSE\n",
"WIN\n0 0 0 \n",
"WIN\n1 5 1 5 \n",
"LOSE\n"
]
}
|
{
"input": [
"2 1\n1000000000 1000000000\n2 1\n",
"3 2\n123 345 567\n1 2\n3 2\n",
"3 0\n4 1 4\n",
"3 2\n1 0 1\n3 2\n2 1\n",
"3 0\n1 2 3\n",
"3 2\n1 1 1\n1 2\n2 3\n",
"2 0\n1000000000 1000000000\n",
"2 0\n123456789 987654321\n",
"3 2\n123 345 123\n1 2\n3 2\n",
"1 0\n271828182\n",
"2 1\n1000000000 1000000000\n1 2\n",
"6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n",
"3 2\n2 3 4\n1 2\n1 3\n",
"1 0\n0\n"
],
"output": [
"WIN\n0 0 \n",
"WIN\n123 0 123 \n",
"WIN\n4 0 4 \n",
"LOSE\n",
"LOSE\n",
"WIN\n1 0 1 \n",
"LOSE\n",
"WIN\n123456789 123456789 \n",
"WIN\n123 0 123 \n",
"WIN\n0 \n",
"WIN\n0 0 \n",
"WIN\n678678678 395063145 1073741823 2 3 1 \n",
"WIN\n0 4 4 \n",
"LOSE\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
int n, m, h[N], lev[N], Xor[N], cnt[N], deg[N];
vector<int> nxt[N], rnxt[N], vec[N];
set<int> s;
queue<int> q;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> h[i];
for (int i = 1; i <= m; ++i) {
int x, y;
cin >> x >> y;
rnxt[y].push_back(x);
nxt[x].push_back(y);
++deg[x];
}
for (int i = 1; i <= n; ++i)
if (!deg[i]) q.push(i);
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto v : nxt[u]) ++cnt[lev[v]];
while (cnt[lev[u]]) ++lev[u];
Xor[lev[u]] ^= h[u];
for (auto v : nxt[u]) --cnt[lev[v]];
for (auto v : rnxt[u])
if (!--deg[v]) q.push(v);
}
for (int i = n; i; --i)
if (Xor[i]) {
puts("WIN");
for (int u = 1; u <= n; ++u)
if (lev[u] == i && h[u] > (h[u] ^ Xor[i])) {
h[u] ^= Xor[i];
for (auto v : nxt[u])
if (Xor[lev[v]]) h[v] ^= Xor[lev[v]], Xor[lev[v]] = 0;
}
for (int j = 1; j <= n; ++j) cout << h[j] << " ";
puts("");
return 0;
}
puts("LOSE");
return 0;
}
|
1149_E. Election Promises
|
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes.
There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles — it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax.
Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles.
The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves?
Input
The first line of the input contains two integers n, m (1 ≤ n ≤ 200 000, 0 ≤ m ≤ 200 000) — the number of cities and roads in Byteland.
The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≤ h_i ≤ 10^9); h_i denotes the amount of taxes paid in the i-th city.
Each of the following m lines contains two integers (1 ≤ u, v ≤ n, u ≠ v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities.
We can show that the conventions cannot be held indefinitely for any correct test case.
Output
If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≤ h'_i ≤ 2 ⋅ 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 ⋅ 10^{18} bourles.
If the party cannot assure their victory, output LOSE in the first and only line of the output.
Examples
Input
4 2
2 1 1 5
1 2
3 4
Output
WIN
1 5 1 5
Input
4 2
1 5 1 5
1 2
3 4
Output
LOSE
Input
3 3
314 159 265
1 2
1 3
3 2
Output
WIN
0 0 0
Input
6 4
2 2 5 5 6 6
1 3
2 4
3 5
4 6
Output
LOSE
Note
In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally.
The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win.
In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
|
{
"input": [
"6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n",
"3 3\n314 159 265\n1 2\n1 3\n3 2\n",
"4 2\n2 1 1 5\n1 2\n3 4\n",
"4 2\n1 5 1 5\n1 2\n3 4\n"
],
"output": [
"LOSE\n",
"WIN\n0 0 0 \n",
"WIN\n1 5 1 5 \n",
"LOSE\n"
]
}
|
{
"input": [
"2 1\n1000000000 1000000000\n2 1\n",
"3 2\n123 345 567\n1 2\n3 2\n",
"3 0\n4 1 4\n",
"3 2\n1 0 1\n3 2\n2 1\n",
"3 0\n1 2 3\n",
"3 2\n1 1 1\n1 2\n2 3\n",
"2 0\n1000000000 1000000000\n",
"2 0\n123456789 987654321\n",
"3 2\n123 345 123\n1 2\n3 2\n",
"1 0\n271828182\n",
"2 1\n1000000000 1000000000\n1 2\n",
"6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n",
"3 2\n2 3 4\n1 2\n1 3\n",
"1 0\n0\n"
],
"output": [
"WIN\n0 0 \n",
"WIN\n123 0 123 \n",
"WIN\n4 0 4 \n",
"LOSE\n",
"LOSE\n",
"WIN\n1 0 1 \n",
"LOSE\n",
"WIN\n123456789 123456789 \n",
"WIN\n123 0 123 \n",
"WIN\n0 \n",
"WIN\n0 0 \n",
"WIN\n678678678 395063145 1073741823 2 3 1 \n",
"WIN\n0 4 4 \n",
"LOSE\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int read() {
bool f = 1;
int x = 0;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = 0;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) - 48 + c;
c = getchar();
}
return f ? x : x * -1;
}
char cr[200];
int tt;
inline void print(register int x, register char k = '\n') {
if (!x) putchar('0');
if (x < 0) putchar('-'), x = -x;
while (x) cr[++tt] = x % 10 + '0', x /= 10;
while (tt) putchar(cr[tt--]);
putchar(k);
}
const int maxn = 2e5 + 7;
vector<int> e[maxn], e0[maxn];
queue<int> q;
int a[maxn], mex[maxn], nim[maxn], n, m;
int vis[maxn], wh[maxn];
bool dfs(int u) {
vis[u] = -1;
for (int v : e[u]) {
if (vis[v] == -1) return 0;
if (vis[v] == 0)
if (!dfs(v)) return 0;
}
vis[u] = 1;
q.push(u);
return 1;
}
signed main() {
n = read();
m = read();
for (int i = 1; i <= n; i++) {
a[i] = read();
}
for (int i = 1; i <= m; i++) {
int u = read(), v = read();
e[u].push_back(v);
e0[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) dfs(i);
}
for (int i = 1; i <= n; i++) {
int u = q.front();
q.pop();
mex[u] = 1;
for (int v : e[u]) wh[mex[v]] = 1;
while (wh[mex[u]]) {
mex[u]++;
}
nim[mex[u]] ^= a[u];
for (int v : e[u]) wh[mex[v]] = 0;
}
bool f = 0;
int tmp = 0;
for (int i = n; i; i--) {
if (nim[i]) {
f = 1;
tmp = i;
break;
}
}
if (!f) {
puts("LOSS");
return 0;
}
puts("WIN");
for (int u = 1; u <= n; u++) {
if (mex[u] == tmp && a[u] ^ nim[mex[u]] < a[u]) {
for (int v : e[u]) {
a[v] = nim[mex[v]] ^= a[v];
}
a[u] = nim[mex[u]] ^= a[u];
for (int i = 1; i <= n; i++) {
print(a[i], ' ');
}
return 0;
}
}
return 0;
}
|
1149_E. Election Promises
|
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes.
There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles — it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax.
Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles.
The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves?
Input
The first line of the input contains two integers n, m (1 ≤ n ≤ 200 000, 0 ≤ m ≤ 200 000) — the number of cities and roads in Byteland.
The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≤ h_i ≤ 10^9); h_i denotes the amount of taxes paid in the i-th city.
Each of the following m lines contains two integers (1 ≤ u, v ≤ n, u ≠ v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities.
We can show that the conventions cannot be held indefinitely for any correct test case.
Output
If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≤ h'_i ≤ 2 ⋅ 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 ⋅ 10^{18} bourles.
If the party cannot assure their victory, output LOSE in the first and only line of the output.
Examples
Input
4 2
2 1 1 5
1 2
3 4
Output
WIN
1 5 1 5
Input
4 2
1 5 1 5
1 2
3 4
Output
LOSE
Input
3 3
314 159 265
1 2
1 3
3 2
Output
WIN
0 0 0
Input
6 4
2 2 5 5 6 6
1 3
2 4
3 5
4 6
Output
LOSE
Note
In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally.
The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win.
In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
|
{
"input": [
"6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n",
"3 3\n314 159 265\n1 2\n1 3\n3 2\n",
"4 2\n2 1 1 5\n1 2\n3 4\n",
"4 2\n1 5 1 5\n1 2\n3 4\n"
],
"output": [
"LOSE\n",
"WIN\n0 0 0 \n",
"WIN\n1 5 1 5 \n",
"LOSE\n"
]
}
|
{
"input": [
"2 1\n1000000000 1000000000\n2 1\n",
"3 2\n123 345 567\n1 2\n3 2\n",
"3 0\n4 1 4\n",
"3 2\n1 0 1\n3 2\n2 1\n",
"3 0\n1 2 3\n",
"3 2\n1 1 1\n1 2\n2 3\n",
"2 0\n1000000000 1000000000\n",
"2 0\n123456789 987654321\n",
"3 2\n123 345 123\n1 2\n3 2\n",
"1 0\n271828182\n",
"2 1\n1000000000 1000000000\n1 2\n",
"6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n",
"3 2\n2 3 4\n1 2\n1 3\n",
"1 0\n0\n"
],
"output": [
"WIN\n0 0 \n",
"WIN\n123 0 123 \n",
"WIN\n4 0 4 \n",
"LOSE\n",
"LOSE\n",
"WIN\n1 0 1 \n",
"LOSE\n",
"WIN\n123456789 123456789 \n",
"WIN\n123 0 123 \n",
"WIN\n0 \n",
"WIN\n0 0 \n",
"WIN\n678678678 395063145 1073741823 2 3 1 \n",
"WIN\n0 4 4 \n",
"LOSE\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, eid, cnt, p[N], h[N], d[N], vis[N], sg[N], sum[N], b[N];
struct edge {
int V, nxt;
} e[N << 1];
inline void addedge(int u, int v) { e[++eid] = (edge){v, p[u]}, p[u] = eid; }
inline void tuopu() {
queue<int> q;
while (!q.empty()) q.pop();
for (int i = (1); i <= (n); i++)
if (d[i] == 0) q.push(i);
while (!q.empty()) {
int u = q.front();
q.pop(), b[++cnt] = u;
for (int i = p[u], v = e[i].V; i; i = e[i].nxt, v = e[i].V)
if (!--d[v]) q.push(v);
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = (1); i <= (n); i++) scanf("%d", &h[i]);
for (int i = (1); i <= (m); i++) {
int u, v;
scanf("%d%d", &u, &v);
addedge(u, v), d[v]++;
}
tuopu();
for (int i = (cnt); i >= (1); i--) {
int u = b[i];
for (int j = p[u], v = e[j].V; j; j = e[j].nxt, v = e[j].V) vis[sg[v]] = u;
for (int j = (0); j <= (n); j++)
if (vis[j] * u) {
sum[sg[u] = j] ^= h[u];
break;
}
}
for (int i = (n - 1); i >= (0); i--)
if (sum[i]) {
puts("WIN");
for (int u = (1); u <= (n); u++)
if (sg[u] == i && (h[u] ^ sum[i]) < h[u]) {
h[u] ^= sum[i];
for (int j = p[u], v = e[j].V; j; j = e[j].nxt, v = e[j].V)
h[v] ^= sum[sg[v]], sum[sg[v]] = 0;
for (int j = (1); j <= (n); j++) printf("%d ", h[j]);
return 0;
}
}
puts("LOSE");
return 0;
}
|
1149_E. Election Promises
|
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes.
There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles — it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax.
Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles.
The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves?
Input
The first line of the input contains two integers n, m (1 ≤ n ≤ 200 000, 0 ≤ m ≤ 200 000) — the number of cities and roads in Byteland.
The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≤ h_i ≤ 10^9); h_i denotes the amount of taxes paid in the i-th city.
Each of the following m lines contains two integers (1 ≤ u, v ≤ n, u ≠ v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities.
We can show that the conventions cannot be held indefinitely for any correct test case.
Output
If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≤ h'_i ≤ 2 ⋅ 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 ⋅ 10^{18} bourles.
If the party cannot assure their victory, output LOSE in the first and only line of the output.
Examples
Input
4 2
2 1 1 5
1 2
3 4
Output
WIN
1 5 1 5
Input
4 2
1 5 1 5
1 2
3 4
Output
LOSE
Input
3 3
314 159 265
1 2
1 3
3 2
Output
WIN
0 0 0
Input
6 4
2 2 5 5 6 6
1 3
2 4
3 5
4 6
Output
LOSE
Note
In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally.
The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win.
In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
|
{
"input": [
"6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n",
"3 3\n314 159 265\n1 2\n1 3\n3 2\n",
"4 2\n2 1 1 5\n1 2\n3 4\n",
"4 2\n1 5 1 5\n1 2\n3 4\n"
],
"output": [
"LOSE\n",
"WIN\n0 0 0 \n",
"WIN\n1 5 1 5 \n",
"LOSE\n"
]
}
|
{
"input": [
"2 1\n1000000000 1000000000\n2 1\n",
"3 2\n123 345 567\n1 2\n3 2\n",
"3 0\n4 1 4\n",
"3 2\n1 0 1\n3 2\n2 1\n",
"3 0\n1 2 3\n",
"3 2\n1 1 1\n1 2\n2 3\n",
"2 0\n1000000000 1000000000\n",
"2 0\n123456789 987654321\n",
"3 2\n123 345 123\n1 2\n3 2\n",
"1 0\n271828182\n",
"2 1\n1000000000 1000000000\n1 2\n",
"6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n",
"3 2\n2 3 4\n1 2\n1 3\n",
"1 0\n0\n"
],
"output": [
"WIN\n0 0 \n",
"WIN\n123 0 123 \n",
"WIN\n4 0 4 \n",
"LOSE\n",
"LOSE\n",
"WIN\n1 0 1 \n",
"LOSE\n",
"WIN\n123456789 123456789 \n",
"WIN\n123 0 123 \n",
"WIN\n0 \n",
"WIN\n0 0 \n",
"WIN\n678678678 395063145 1073741823 2 3 1 \n",
"WIN\n0 4 4 \n",
"LOSE\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
struct Edge {
int to, next;
} edge[N * 2];
int head[N], num;
void add_edge(int a, int b) { edge[++num] = (Edge){b, head[a]}, head[a] = num; }
int sg[N];
int dfs(int x) {
if (sg[x] != -1) return sg[x];
vector<int> tmp;
for (int i = head[x]; i; i = edge[i].next) tmp.push_back(dfs(edge[i].to));
sort(tmp.begin(), tmp.end());
if (!tmp.size())
sg[x] = 0;
else {
for (int i = 0; i <= tmp.size() - 1; i++)
if (i != tmp[i]) {
sg[x] = i;
break;
}
if (sg[x] == -1) sg[x] = tmp.size();
}
return sg[x];
}
long long h[N], sh[N];
bool vv[N];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%lld", &h[i]);
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d%d", &a, &b);
add_edge(a, b);
}
memset(sg, -1, sizeof(sg));
for (int i = 1; i <= n; i++) sh[dfs(i)] ^= h[i];
bool flag = false;
for (int i = 1; i <= n; i++)
if (sh[i]) flag = true;
if (flag) {
printf("WIN\n");
int tmp = -1;
for (int i = 1; i <= n; i++)
if (tmp == -1 || sg[tmp] < sg[i] || (sg[tmp] == sg[i] && h[tmp] < h[i]))
tmp = i;
h[tmp] ^= sh[sg[tmp]];
for (int i = head[tmp]; i; i = edge[i].next)
if (!vv[sg[edge[i].to]]) {
vv[sg[edge[i].to]] = true;
h[edge[i].to] ^= sh[sg[edge[i].to]];
}
for (int i = 1; i <= n; i++) printf("%d ", h[i]);
printf("\n");
} else
printf("LOSE\n");
return 0;
}
|
1149_E. Election Promises
|
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes.
There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles — it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax.
Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles.
The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves?
Input
The first line of the input contains two integers n, m (1 ≤ n ≤ 200 000, 0 ≤ m ≤ 200 000) — the number of cities and roads in Byteland.
The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≤ h_i ≤ 10^9); h_i denotes the amount of taxes paid in the i-th city.
Each of the following m lines contains two integers (1 ≤ u, v ≤ n, u ≠ v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities.
We can show that the conventions cannot be held indefinitely for any correct test case.
Output
If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≤ h'_i ≤ 2 ⋅ 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 ⋅ 10^{18} bourles.
If the party cannot assure their victory, output LOSE in the first and only line of the output.
Examples
Input
4 2
2 1 1 5
1 2
3 4
Output
WIN
1 5 1 5
Input
4 2
1 5 1 5
1 2
3 4
Output
LOSE
Input
3 3
314 159 265
1 2
1 3
3 2
Output
WIN
0 0 0
Input
6 4
2 2 5 5 6 6
1 3
2 4
3 5
4 6
Output
LOSE
Note
In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally.
The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win.
In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
|
{
"input": [
"6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n",
"3 3\n314 159 265\n1 2\n1 3\n3 2\n",
"4 2\n2 1 1 5\n1 2\n3 4\n",
"4 2\n1 5 1 5\n1 2\n3 4\n"
],
"output": [
"LOSE\n",
"WIN\n0 0 0 \n",
"WIN\n1 5 1 5 \n",
"LOSE\n"
]
}
|
{
"input": [
"2 1\n1000000000 1000000000\n2 1\n",
"3 2\n123 345 567\n1 2\n3 2\n",
"3 0\n4 1 4\n",
"3 2\n1 0 1\n3 2\n2 1\n",
"3 0\n1 2 3\n",
"3 2\n1 1 1\n1 2\n2 3\n",
"2 0\n1000000000 1000000000\n",
"2 0\n123456789 987654321\n",
"3 2\n123 345 123\n1 2\n3 2\n",
"1 0\n271828182\n",
"2 1\n1000000000 1000000000\n1 2\n",
"6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n",
"3 2\n2 3 4\n1 2\n1 3\n",
"1 0\n0\n"
],
"output": [
"WIN\n0 0 \n",
"WIN\n123 0 123 \n",
"WIN\n4 0 4 \n",
"LOSE\n",
"LOSE\n",
"WIN\n1 0 1 \n",
"LOSE\n",
"WIN\n123456789 123456789 \n",
"WIN\n123 0 123 \n",
"WIN\n0 \n",
"WIN\n0 0 \n",
"WIN\n678678678 395063145 1073741823 2 3 1 \n",
"WIN\n0 4 4 \n",
"LOSE\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = (int)2e5 + 5;
int n, m;
vector<int> v[MAX];
int h[MAX];
set<int> s[MAX];
set<int> t[MAX];
int dep[MAX];
int xors[MAX];
vector<pair<int, int> > mxx[MAX];
int bio[MAX];
void dfs(int cx) {
if (bio[cx]) return;
bio[cx] = 1;
int p[1000];
for (int i = (0), _for = (1000); i < _for; ++i) p[i] = 0;
for (auto first : v[cx]) {
p[dep[first]] = 1;
}
for (int i = (0), _for = (1000); i < _for; ++i) {
if (!p[i]) {
dep[cx] = i;
xors[i] ^= h[cx];
mxx[i].push_back({h[cx], cx});
break;
}
}
for (auto nx : t[cx]) {
s[nx].erase(cx);
if (s[nx].empty()) dfs(nx);
}
}
int find_cx(int i) {
int xr = xors[i];
for (auto cx : mxx[i]) {
if ((cx.first & xr) <= cx.first) return cx.second;
}
return -1;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = (0), _for = (n); i < _for; ++i) scanf("%d", h + i);
for (int i = (0), _for = (m); i < _for; ++i) {
int a, b;
scanf("%d%d", &a, &b);
a--;
b--;
v[a].push_back(b);
s[a].insert(b);
t[b].insert(a);
}
for (int i = (0), _for = (n); i < _for; ++i)
if (s[i].empty()) dfs(i);
for (int i = 999; i >= 0; --i)
if (xors[i]) {
puts("WIN");
int cx = find_cx(i);
h[cx] ^= xors[i];
for (auto nx : v[cx]) {
int d = dep[nx];
h[nx] ^= xors[d];
xors[d] = 0;
}
for (int i = (0), _for = (n); i < _for; ++i) {
printf("%d ", h[i]);
}
printf("\n");
return 0;
}
puts("LOSE");
return 0;
}
|
1149_E. Election Promises
|
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes.
There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles — it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax.
Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles.
The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves?
Input
The first line of the input contains two integers n, m (1 ≤ n ≤ 200 000, 0 ≤ m ≤ 200 000) — the number of cities and roads in Byteland.
The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≤ h_i ≤ 10^9); h_i denotes the amount of taxes paid in the i-th city.
Each of the following m lines contains two integers (1 ≤ u, v ≤ n, u ≠ v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities.
We can show that the conventions cannot be held indefinitely for any correct test case.
Output
If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≤ h'_i ≤ 2 ⋅ 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 ⋅ 10^{18} bourles.
If the party cannot assure their victory, output LOSE in the first and only line of the output.
Examples
Input
4 2
2 1 1 5
1 2
3 4
Output
WIN
1 5 1 5
Input
4 2
1 5 1 5
1 2
3 4
Output
LOSE
Input
3 3
314 159 265
1 2
1 3
3 2
Output
WIN
0 0 0
Input
6 4
2 2 5 5 6 6
1 3
2 4
3 5
4 6
Output
LOSE
Note
In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally.
The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win.
In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
|
{
"input": [
"6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n",
"3 3\n314 159 265\n1 2\n1 3\n3 2\n",
"4 2\n2 1 1 5\n1 2\n3 4\n",
"4 2\n1 5 1 5\n1 2\n3 4\n"
],
"output": [
"LOSE\n",
"WIN\n0 0 0 \n",
"WIN\n1 5 1 5 \n",
"LOSE\n"
]
}
|
{
"input": [
"2 1\n1000000000 1000000000\n2 1\n",
"3 2\n123 345 567\n1 2\n3 2\n",
"3 0\n4 1 4\n",
"3 2\n1 0 1\n3 2\n2 1\n",
"3 0\n1 2 3\n",
"3 2\n1 1 1\n1 2\n2 3\n",
"2 0\n1000000000 1000000000\n",
"2 0\n123456789 987654321\n",
"3 2\n123 345 123\n1 2\n3 2\n",
"1 0\n271828182\n",
"2 1\n1000000000 1000000000\n1 2\n",
"6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n",
"3 2\n2 3 4\n1 2\n1 3\n",
"1 0\n0\n"
],
"output": [
"WIN\n0 0 \n",
"WIN\n123 0 123 \n",
"WIN\n4 0 4 \n",
"LOSE\n",
"LOSE\n",
"WIN\n1 0 1 \n",
"LOSE\n",
"WIN\n123456789 123456789 \n",
"WIN\n123 0 123 \n",
"WIN\n0 \n",
"WIN\n0 0 \n",
"WIN\n678678678 395063145 1073741823 2 3 1 \n",
"WIN\n0 4 4 \n",
"LOSE\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int level[200005];
int h[200005];
int val[200005];
bool vis[200005];
bool num[200005];
vector<int> E[200005];
void dfs(int x) {
if (vis[x]) return;
vis[x] = true;
if (E[x].size() == 0) level[x] = 0;
num[0] = true;
int sz = E[x].size();
for (int i = 1; i <= sz + 1; i++) num[i] = true;
for (int v : E[x]) {
if (!vis[v]) {
dfs(v);
}
num[level[v]] = false;
}
for (int i = 0; i <= sz + 1; i++) {
if (num[i]) {
level[x] = i;
return;
}
}
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &h[i]);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
E[u].push_back(v);
}
for (int i = 1; i <= n; i++) level[i] = -1;
for (int i = 1; i <= n; i++) vis[i] = false;
for (int i = 1; i <= n; i++) {
if (!vis[i]) dfs(i);
}
int mx = 0;
for (int i = 1; i <= n; i++) {
mx = max(mx, level[i]);
}
for (int i = 0; i <= mx; i++) val[i] = 0;
int choose = 0;
for (int i = 1; i <= n; i++) {
val[level[i]] ^= h[i];
}
bool flag = true;
for (int i = 0; i <= mx; i++)
if (val[i] != 0) flag = false;
if (flag) {
puts("LOSE");
return 0;
}
for (int i = 1; i <= n; i++) {
if (level[i] == mx) {
if ((val[mx] ^ h[i]) < h[i]) {
choose = i;
break;
}
}
}
h[choose] = val[mx] ^ h[choose];
for (int v : E[choose]) {
int x = level[v];
h[v] = val[x] ^ h[v];
}
puts("WIN");
for (int i = 1; i <= n; i++) {
printf("%d ", h[i]);
}
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python3
|
m = int(input())
l = [0 for _ in range(m + 1)]
for _ in range(m - 1):
a,b = map(int, input().split())
l[a] += 1
l[b] += 1
if 2 in l:
print("NO")
else:
print("YES")
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class D1_AddOnATree {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader inp = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
solver.solve(inp, out);
out.close();
}
private static class Solver {
private void solve(InputReader inp, PrintWriter out) {
int n = inp.nextInt();
ArrayList<Integer>[] tree = new ArrayList[n];
for (int i = 0; i < n; i++) tree[i] = new ArrayList<>();
for (int i = 0; i < n - 1; i++) {
int u = inp.nextInt() - 1, v = inp.nextInt() - 1;
tree[u].add(v);
tree[v].add(u);
}
for (int i = 0; i < n; i++) {
if (tree[i].size() == 2) {
out.print("NO");
return;
}
}
out.print("YES");
}
}
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
void pr_init() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
vector<vector<long long> > tree;
vector<long long> deg;
void solve() {
long long n;
cin >> n;
tree.assign(n + 1, vector<long long>());
deg.assign(n + 1, 0);
for (long long i = 0; i < n - 1; i++) {
long long u, v;
cin >> u >> v;
deg[u]++;
deg[v]++;
tree[u].emplace_back(v);
tree[v].emplace_back(u);
}
if (n == 2) {
cout << "YES\n";
return;
} else if (n == 3) {
cout << "NO\n";
return;
}
bool is = true;
for (long long i = 1; i <= n; i++) {
if (deg[i] != 1 && deg[i] < 3) is = false;
}
if (is)
cout << "YES\n";
else
cout << "NO\n";
}
int32_t main() {
solve();
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python3
|
# @author
import sys
class D1AddOnATree:
def dfs(self, start):
self.done[start] = 1
for x in self.adj[start]:
if self.done[x]:
continue
self.par[x] = start
self.dfs(x)
def solve(self):
from collections import defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 5)
n = int(input())
self.adj = defaultdict(list)
self.par = defaultdict(int)
self.done = [0] * (n + 1)
for i in range(n - 1):
u, v = [int(_) for _ in input().split()]
self.adj[u].append(v)
self.adj[v].append(u)
self.done[u] += 1
self.done[v] += 1
for i in range(1, n + 1):
if self.done[i] == 2:
print("NO")
break
else:
print("YES")
return
v = max(len(self.adj[p]) for p in self.adj)
start = -1
for p in self.adj:
if len(self.adj[p]) == v:
start = p
break
assert(start != -1)
self.dfs(start)
cnt = [0] * (n + 1)
for k in self.adj:
if self.par[k] == 0:
continue
if len(self.adj[k]) == 1:
cnt[self.par[k]] += 1
ans = 0
for x in cnt:
if x == 1:
ans += 1
# for x in adj:
# if self.adj
if ans == 0:
print("YES")
else:
print("NO")
solver = D1AddOnATree()
input = sys.stdin.readline
solver.solve()
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt();
int[] deg = new int[n--];
while (n-- > 0) {
int u = sc.nextInt() - 1;
int v = sc.nextInt() - 1;
deg[u]++;
deg[v]++;
}
for (int x : deg)
if (x==2){
System.out.println("NO");
return;
}
System.out.println("YES");
out.flush();
out.close();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public double[] nextDoubleArray(int n) throws IOException {
double[] ans = new double[n];
for (int i = 0; i < n; i++)
ans[i] = nextDouble();
return ans;
}
public short nextShort() throws IOException {
return Short.parseShort(next());
}
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class R572D
{
public static void main(String[] args)
{
JS scan = new JS();
int n = scan.nextInt();
ArrayList<Integer>[] graph = new ArrayList[n];
for(int i = 0; i < n; i++) graph[i] = new ArrayList<Integer>();
for(int i = 0; i < n-1; i++) {
int u = scan.nextInt()-1;
int v = scan.nextInt()-1;
graph[u].add(v);
graph[v].add(u);
}
for(int i = 0; i < n; i++) {
if(graph[i].size() == 2) {
System.out.println("NO");
return;
}
}
System.out.println("YES");
}
static class JS{
public int BS = 1<<16;
public char NC = (char)0;
byte[] buf = new byte[BS];
int bId = 0, size = 0;
char c = NC;
double num = 1;
BufferedInputStream in;
public JS() {
in = new BufferedInputStream(System.in, BS);
}
public JS(String s) throws FileNotFoundException {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
public char nextChar(){
while(bId==size) {
try {
size = in.read(buf);
}catch(Exception e) {
return NC;
}
if(size==-1)return NC;
bId=0;
}
return (char)buf[bId++];
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
num=1;
boolean neg = false;
if(c==NC)c=nextChar();
for(;(c<'0' || c>'9'); c = nextChar()) {
if(c=='-')neg=true;
}
long res = 0;
for(; c>='0' && c <='9'; c=nextChar()) {
res = (res<<3)+(res<<1)+c-'0';
num*=10;
}
return neg?-res:res;
}
public double nextDouble() {
double cur = nextLong();
return c!='.' ? cur:cur+nextLong()/num;
}
public String next() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c>32) {
res.append(c);
c=nextChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c!='\n') {
res.append(c);
c=nextChar();
}
return res.toString();
}
public boolean hasNext() {
if(c>32)return true;
while(true) {
c=nextChar();
if(c==NC)return false;
else if(c>32)return true;
}
}
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python3
|
n=int(input())
deg=[0]*n
for i in range(n-1):
u,v=map(int,input().split())
u-=1
v-=1
deg[u]+=1
deg[v]+=1
for d in deg:
if d==2:
print("NO")
exit(0)
print("YES")
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python3
|
from collections import defaultdict, deque
from heapq import heappush, heappop
from math import inf
ri = lambda : map(int, input().split())
def solve():
n = int(input())
cnt = defaultdict(int)
for _ in range(n-1):
x,y = ri()
cnt[x] += 1
cnt[y] += 1
valid = True
for x in cnt:
if cnt[x] == 2:
valid = False
if valid:
print("YES")
else:
print("NO")
t = 1
#t = int(input())
while t:
t -= 1
solve()
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python3
|
n = int(input())
tree_nums = [0 for i in range(n)]
for i in range(n - 1):
a1, a2 = map(int, input().split())
tree_nums[a1 - 1] += 1
tree_nums[a2 - 1] += 1
if n == 2:
print('YES')
elif n == 3:
print('NO')
else:
ans_is_no = False
for i in range(n):
if tree_nums[i] == 2:
ans_is_no = True
print('NO')
break
if not ans_is_no:
print('YES')
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.util.*;
import java.io.*;
import static java.lang.Math.*;
import java.math.BigInteger;
public class D {
public static void main(String[] args) throws Exception {
FastScanner scanner = new FastScanner();
int n = scanner.nextInt();
int[] cnt = new int[n];
for (int i = 0; i < n - 1; i++) {
int[] tmp = scanner.nextIntArray();
cnt[tmp[0] - 1]++;
cnt[tmp[1] - 1]++;
}
for (int i = 0; i < n; i++) {
if (cnt[i] == 2) {
System.out.println("NO");
System.exit(0);
}
}
System.out.println("YES");
}
private static class FastScanner {
private BufferedReader br;
public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); }
public FastScanner(String input) throws Exception { br = new BufferedReader(new FileReader(input)); }
public int[] nextIntArray() throws IOException {
String line = br.readLine();
String[] strings = line.trim().split("\\s+");
int[] array = new int[strings.length];
for (int i = 0; i < array.length; i++)
array[i] = Integer.parseInt(strings[i]);
return array;
}
public int nextInt() throws IOException { return Integer.parseInt(br.readLine()); }
public long[] nextLongArray() throws IOException {
String line = br.readLine();
String[] strings = line.trim().split("\\s+");
long[] array = new long[strings.length];
for (int i = 0; i < array.length; i++)
array[i] = Long.parseLong(strings[i]);
return array;
}
public ArrayList<Integer> nextIntegerArrayList() throws IOException {
String line = br.readLine();
String[] strings = line.trim().split("\\s+");
ArrayList<Integer> array = new ArrayList<Integer>();
for (int i = 0; i < strings.length; i++)
array.add(Integer.parseInt(strings[i]));
return array;
}
public ArrayList<Long> nextLongArrayList() throws IOException {
String line = br.readLine();
String[] strings = line.trim().split("\\s+");
ArrayList<Long> array = new ArrayList<Long>();
for (int i = 0; i < strings.length; i++)
array.add(Long.parseLong(strings[i]));
return array;
}
public long nextLong() throws IOException { return Long.parseLong(br.readLine()); }
public double nextDouble() throws IOException { return Double.parseDouble(br.readLine()); }
public String nextLine() throws IOException { return br.readLine(); }
public ArrayList<BigInteger> nextBigIntegers() throws IOException {
String line = br.readLine();
String[] strings = line.trim().split("\\s+");
ArrayList<BigInteger> array = new ArrayList<BigInteger>();
for (int i = 0; i < strings.length; i++)
array.add(BigInteger.valueOf(Long.valueOf(strings[i])));
return array;
}
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
public class D {
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static MyScanner sc = new MyScanner();
public static void main(String[] args) {
doTask();
out.flush();
}
public static void doTask(){
int n = sc.nextInt();
int[] nodes = new int[n + 1];
int v,u;
for (int i = 0; i<n-1;i++) {
v = sc.nextInt();
u = sc.nextInt();
nodes[v]++;
nodes[u]++;
}
boolean res = true;
for (int i = 1; i<=n;i++) {
// result += Math.max(0, (nodes[i] + 1)/2 - 1);
if (nodes[i] == 2) {
res = false;
}
}
out.println(res ? "YES" : "NO");
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int ind[n];
memset(ind, 0, sizeof(ind));
int u, v;
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
u--;
v--;
ind[u]++;
ind[v]++;
}
bool f = 1;
int c1 = 0, c2 = 0;
for (int i = 0; i < n; i++) {
if (ind[i] == 2) {
return cout << "NO", 0;
}
}
cout << "YES";
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author revanth
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
D1AddOnATree solver = new D1AddOnATree();
solver.solve(1, in, out);
out.close();
}
static class D1AddOnATree {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt(), u, v;
ArrayList<Integer>[] adj = new ArrayList[n + 1];
for (int i = 1; i <= n; i++)
adj[i] = new ArrayList<>();
for (int i = 0; i < n - 1; i++) {
u = in.nextInt();
v = in.nextInt();
adj[u].add(v);
adj[v].add(u);
}
for (int i = 1; i <= n; i++) {
if (adj[i].size() == 2) {
out.print("NO");
return;
}
}
out.print("YES");
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar;
private int snumChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void close() {
writer.close();
}
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
//https://codeforces.com/contest/1189/problem/D1
// package Codeforces;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
public class cf_572_2_d1 {
public static void main(String[] args) throws Exception {
FastScanner f = new FastScanner();
// int T = f.nextInt();
// while (T-- > 0) {
// int n = f.nextInt();
// int[] a = f.nextIntArray(n);
//
// }
int n = f.nextInt();
int[] a = new int[n+1];
for (int i = 0; i < n + 1; i++) {
a[i]=0;
}
for (int i = 0; i < n - 1; i++) {
a[f.nextInt()]++;
a[f.nextInt()]++;
}
boolean flag = true;
for (int i = 1; i < n+1; i++) {
if (a[i]==2)
{
flag= false;
break;
}
}
if (flag)
System.out.println("YES");
else
System.out.println("NO");
/*
ArrayList<Integer>[] a = new ArrayList[n+1];
for (int i = 0; i < n+1; i++) {
a[i] = new ArrayList<Integer>();
}
for (int i = 0; i < n-1; i++) {
int u = f.nextInt();
int v = f.nextInt();
a[u].add(v);
a[v].add(u);
}
boolean flag = true;
if (n==2)
System.out.println("YES");
else if (n==3)
System.out.println("NO");
else
{
for (int i = 1; i < n+1; i++) {
if (a[i].size()==1)
{
int parent = a[i].get(0);
if (a[parent].size()==2)
{
flag = false;
break;
}
}
}
if (flag)
System.out.println("YES");
else
System.out.println("NO");
}*/
}
}
class FastScanner {
public int BS = 1<<16;
public char NC = (char)0;
byte[] buf = new byte[BS];
int bId = 0, size = 0;
char c = NC;
double num = 1;
BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
public char nextChar(){
while(bId==size) {
try {
size = in.read(buf);
}catch(Exception e) {
return NC;
}
if(size==-1)return NC;
bId=0;
}
return (char)buf[bId++];
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
num=1;
boolean neg = false;
if(c==NC)c=nextChar();
for(;(c<'0' || c>'9'); c = nextChar()) {
if(c=='-')neg=true;
}
long res = 0;
for(; c>='0' && c <='9'; c=nextChar()) {
res = (res<<3)+(res<<1)+c-'0';
num*=10;
}
return neg?-res:res;
}
public double nextDouble() {
double cur = nextLong();
return c!='.' ? cur:cur+nextLong()/num;
}
public String next() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c>32) {
res.append(c);
c=nextChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c!='\n') {
res.append(c);
c=nextChar();
}
return res.toString();
}
public boolean hasNext() {
if(c>32)return true;
while(true) {
c=nextChar();
if(c==NC)return false;
else if(c>32)return true;
}
}
public int[] nextIntArray(int n) {
int[] res = new int[n];
for(int i = 0; i < n; i++) res[i] = nextInt();
return res;
}
public char[] nextCharArray(int n) {
char[] res = new char[n];
for (int i = 0; i < n; i++) {
res[i] = nextChar();
}
return res;
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python3
|
n = int(input())
gr = {i: 0 for i in range(1, n + 1)}
for i in range(n - 1):
a, b = map(int, input().split())
gr[a] += 1
gr[b] += 1
for i in gr:
if gr[i] == 2:
print('NO')
exit(0)
print('YES')
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python3
|
n = int(input())
arr = [0] * (n+1)
for _ in range(n-1):
a,b = map(int,input().split())
arr[a] += 1
arr[b] += 1
for i in range(1,n+1):
if arr[i] == 2:
print("NO")
break
else:
print("YES")
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.util.*;
import java.io.*;
public class Dy {
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[1000000]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
static void oracle(int graph[][], int dist[][], int V)
{
int i, j, k;
for (i = 1; i < V; i++)
for (j = 1; j < V; j++)
dist[i][j] = graph[i][j];
for (k = 1; k < V; k++)
{
for (i = 1; i < V; i++)
{
for (j = 1; j < V; j++)
{
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
public static void main(String args[])throws IOException{
Reader sc=new Reader();
// Scanner sc=new Scanner(System.in);
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int i,j;
int n=sc.nextInt();
int a[]=new int[n+1];
for(i=0;i<=n;i++)
a[i]=0;
// int
for(i=0;i<n-1;i++){
// a[i]=sc.nextInt();
int u=sc.nextInt();
int v=sc.nextInt();
a[u]++;
a[v]++;
}
long c=0;
int flag=0;
for(i=1;i<=n;i++){
if(a[i]==1)
c++;
if(a[i]==2)
flag=1;
}
if(c*(c-1)/2>=n-1&&flag==0)
pw.println("YES");
else
pw.println("NO");
pw.close();
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python2
|
v = int(raw_input())
adj = []
for k in xrange(v):
adj.append([])
for i in xrange(v-1):
a, b = map(int, raw_input().split())
adj[a-1].append(b)
adj[b-1].append(a)
for k in xrange(v):
if len(adj[k]) == 2:
print "NO"
exit()
print "YES"
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int power(long long int x, long long int y) {
long long int temp;
if (y == 0) return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
bool prime_check(long long int n) {
long long int i, j;
if (n == 1) {
return false;
}
for (i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
long long int fact(long long int n) {
long long int prod = 1;
for (long long int i = 1; i <= n; i++) {
prod = (prod * i) % 1000000007;
}
return prod;
}
bool vowl(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, i, u, v;
cin >> n;
int freq[100003] = {};
for (i = 0; i < n - 1; i++) {
cin >> u >> v;
u--;
v--;
freq[u]++;
freq[v]++;
}
for (i = 0; i < n; i++) {
if (freq[i] == 2) break;
}
if (i == n)
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python3
|
n = int(input())
g = [[] for i in range(n+1)]
d = [0]*100001
for i in range(n-1):
u, v = [int(i) for i in input().split()]
g[u].append(v)
g[v].append(u)
d[u] += 1
d[v] += 1
for i in d:
if i == 2:
print("NO")
break
else:
print("YES")
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python3
|
n = int(input())
edges = [0] * (n)
for i in range(n-1):
a, b = list(map(int, input().split()))
edges[a-1] +=1
edges[b-1] +=1
if 2 in edges:
print("NO")
else:
print("YES")
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, p, q;
vector<int> G[100005];
int main() {
cin >> n;
for (i = 0; i < n - 1; i++) {
cin >> p >> q;
G[p - 1].push_back(q - 1);
G[q - 1].push_back(p - 1);
}
bool flag = false;
for (i = 0; i < n; i++) {
if (G[i].size() == 2) {
flag = true;
break;
}
}
if (!flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int p[] = new int[n];
for (int i = 0; i < n - 1; i++) {
int u = scan.nextInt() - 1;
int v = scan.nextInt() - 1;
p[u]++;
p[v]++;
}
for (int i = 0; i < n; i++) {
if (p[i] == 2) {
System.out.println("NO");
return;
}
}
System.out.println("YES");
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int sc(int &x) { return scanf("%d", &x); }
int sc(unsigned int &x) { return scanf("%u", &x); }
int sc(long long &x) { return scanf("%lld", &x); }
int sc(unsigned long long &x) { return scanf("%llu", &x); }
int sc(double &x) { return scanf("%lf", &x); }
int sc(long double &x) { return scanf("%Lf", &x); }
int sc(char *x) { return scanf("%s", x); }
int sc(char &x) { return scanf("%c", &x); }
template <typename T, typename... Args>
void sc(T &v, Args &...args) {
sc(v);
sc(args...);
}
void pr(const int &x) { printf("%d", x); }
void pr(const unsigned int &x) { printf("%u", x); }
void pr(const long long &x) { printf("%lld", x); }
void pr(const unsigned long long &x) { printf("%llu", x); }
void pr(const double &x) { printf("%.10lf", x); }
void pr(const long double &x) { printf("%.10Lf", x); }
void pr(const char *const &x) { printf("%s", x); }
void pr(char *const &x) { printf("%s", x); }
void pr(const char &x) { printf("%c", x); }
template <typename T, typename... Args>
void pr(T const &v, Args const &...args) {
pr(v);
pr(' ');
pr(args...);
}
const long long MOD = 1e9 + 7;
inline void _add(long long &a, long long b) { a = (a + b) % MOD; }
inline void _sub(long long &a, long long b) { a = (a + MOD - b) % MOD; }
inline void _mul(long long &a, long long b) { a = (long long)a * b % MOD; }
inline long long _Add(long long a, long long b) { return (a + b) % MOD; }
inline long long _Sub(long long a, long long b) { return (a + MOD - b) % MOD; }
inline long long _Mul(long long a, long long b) { return a * b % MOD; }
int deg[100100], n;
int main() {
sc(n);
int u, v;
for (int i = 1, lim = n - 1; i <= lim; ++i) {
sc(u, v);
deg[u]++;
deg[v]++;
}
for (int i = 1, lim = n; i <= lim; ++i) {
if (deg[i] == 2) {
pr("NO");
return 0;
}
}
pr("YES");
pr("\n");
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.util.*;
import java.io.*;
import java.math.*;
public class round572d2d {
public static void main(String args[]) {
FastScanner in = new FastScanner(System.in);
int n = in.nextInt();
int[] vertex = new int[n+1];
for (int i = 0; i < n-1; i++) {
int a = in.nextInt();
int b = in.nextInt();
vertex[a]++;
vertex[b]++;
}
boolean ans = true;
for (int i = 0; i < n+1; i++) {
if (vertex[i] == 2) {
ans = false;
break;
}
}
if (ans) System.out.println("YES");
else System.out.println("NO");
//in.close();
}
// ======================================================================================
// =============================== Reference Code =======================================
// ======================================================================================
static int greatestDivisor(int n) {
int limit = (int) Math.sqrt(n);
int max = 1;
for (int i = 2; i <= limit; i++) {
if (n % i == 0) {
max = Integer.max(max, i);
max = Integer.max(max, n / i);
}
}
return max;
}
// Method to return all primes smaller than or equal to
// n using Sieve of Eratosthenes
static boolean[] sieveOfEratosthenes(int n) {
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed, then it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
// Binary search for number greater than or equal to target
// returns -1 if number not found
private static int bin_gteq(int[] a, int key) {
int low = 0;
int high = a.length;
int max_limit = high;
while (low < high) {
int mid = low + (high - low) / 2;
if (a[mid] < key) {
low = mid + 1;
} else
high = mid;
}
return high == max_limit ? -1 : high;
}
public static int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
static class Tuple<X, Y> {
public final X x;
public final Y y;
public Tuple(X x, Y y) {
this.x = x;
this.y = y;
}
public String toString() {
return "(" + x + "," + y + ")";
}
}
static class Tuple3<X, Y, Z> {
public final X x;
public final Y y;
public final Z z;
public Tuple3(X x, Y y, Z z) {
this.x = x;
this.y = y;
this.z = z;
}
public String toString() {
return "(" + x + "," + y + "," + z + ")";
}
}
static Tuple3<Integer, Integer, Integer> gcdExtended(int a, int b, int x, int y) {
// Base Case
if (a == 0) {
x = 0;
y = 1;
return new Tuple3(0, 1, b);
}
int x1 = 1, y1 = 1; // To store results of recursive call
Tuple3<Integer, Integer, Integer> tuple = gcdExtended(b % a, a, x1, y1);
int gcd = tuple.z;
x1 = tuple.x;
y1 = tuple.y;
// Update x and y using results of recursive
// call
x = y1 - (b / a) * x1;
y = x1;
return new Tuple3(x, y, gcd);
}
// Returns modulo inverse of a
// with respect to m using extended
// Euclid Algorithm. Refer below post for details:
// https://www.geeksforgeeks.org/multiplicative-inverse-under-modulo-m/
static int inv(int a, int m) {
int m0 = m, t, q;
int x0 = 0, x1 = 1;
if (m == 1)
return 0;
// Apply extended Euclid Algorithm
while (a > 1) {
// q is quotient
q = a / m;
t = m;
// m is remainder now, process
// same as euclid's algo
m = a % m;
a = t;
t = x0;
x0 = x1 - q * x0;
x1 = t;
}
// Make x1 positive
if (x1 < 0)
x1 += m0;
return x1;
}
// k is size of num[] and rem[].
// Returns the smallest number
// x such that:
// x % num[0] = rem[0],
// x % num[1] = rem[1],
// ..................
// x % num[k-2] = rem[k-1]
// Assumption: Numbers in num[] are pairwise
// coprime (gcd for every pair is 1)
static int findMinX(int num[], int rem[], int k) {
// Compute product of all numbers
int prod = 1;
for (int i = 0; i < k; i++)
prod *= num[i];
// Initialize result
int result = 0;
// Apply above formula
for (int i = 0; i < k; i++) {
int pp = prod / num[i];
result += rem[i] * inv(pp, num[i]) * pp;
}
return result % prod;
}
/**
* Source: Matt Fontaine
*/
static class FastScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int chars;
public FastScanner(InputStream stream) {
this.stream = stream;
}
int read() {
if (chars == -1)
throw new InputMismatchException();
if (curChar >= chars) {
curChar = 0;
try {
chars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (chars <= 0)
return -1;
}
return buf[curChar++];
}
boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
boolean isEndline(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndline(c));
return res.toString();
}
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main
{
public static void main(String[] args) {
new Thread(null, new Runnable() {
public void run() {
solve();
}
}, "1", 1 << 26).start();
}
static void solve () {
FastReader fr =new FastReader(); PrintWriter op =new PrintWriter(System.out);
int n =fr.nextInt() ,u ,v ,i ,dg[] =new int[n] ;
for (i =1 ; i<n ; ++i) {
u =fr.nextInt()-1 ; v =fr.nextInt()-1 ; dg[u]++ ; dg[v]++ ;
}
for (i =0 ; i<n ; ++i) {
if (dg[i]==2) break;
}
if (i<n) op.println("NO") ; else op.println("YES") ;
op.flush(); op.close();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br =new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st==null || (!st.hasMoreElements()))
{
try
{
st =new StringTokenizer(br.readLine());
}
catch(IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() {
String str ="";
try
{
str =br.readLine();
}
catch(IOException e)
{
e.printStackTrace();
}
return str;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next()) ;
}
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 100005;
long long degree[N];
int main() {
long long n, i, v, u;
cin >> n;
for (i = 0; i < n - 1; i++) {
cin >> v >> u;
degree[v]++;
degree[u]++;
}
for (i = 1; i <= n; i++) {
if (degree[i] == 2) {
cout << "NO\n";
;
return 0;
}
}
cout << "YES\n";
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
const int inff = 0x3f3f3f3f;
const double eqs = 1e-9;
const double E = 2.718281828459;
const double pi = acos(-1.0);
using namespace std;
const int maxn = 1e5 + 5;
int in[maxn];
int main() {
int n, x, y;
cin >> n;
for (int i = 1; i <= n; i++) in[i] = 0;
for (int i = 1; i < n; i++) {
scanf("%d %d", &x, &y);
in[x]++, in[y]++;
}
for (int i = 1; i <= n; i++)
if (in[i] == 2) {
puts("NO");
return 0;
}
puts("YES");
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 5;
int n;
vector<int> G[MAX_N];
bool ans = true;
int main() {
scanf("%d", &n);
for (int i = 0, t1, t2; i < n - 1; i++) {
scanf("%d%d", &t1, &t2);
G[t1].emplace_back(t2);
G[t2].emplace_back(t1);
}
for (int i = 1; i <= n; i++)
if (G[i].size() == 2) {
ans = false;
break;
}
puts(ans ? "YES" : "NO");
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n;
vector<vector<int> > a;
void Input() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
a.resize(n);
for (int i = 1; i <= n - 1; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
a[x].push_back(y);
a[y].push_back(x);
}
}
void Process() {
for (int i = 0; i <= n - 1; i++)
if (a[i].size() == 2) {
cout << "NO";
return;
}
cout << "YES";
}
int main() {
Input();
Process();
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;
public class Cf131 implements Runnable
{
static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int read()
{
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars)
{
curChar = 0;
try
{
numChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
public int nextInt()
{
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
int res = 0;
do
{
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString()
{
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next()
{
return readString();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[]) throws Exception
{
new Thread(null, new Cf131(),"Main",1<<27).start();
}
static class Pair
{
int z;
int o;
Pair(int z,int o)
{
this.z=z;
this.o=o;
}
}
static class Edge implements Comparable<Edge>
{
int end, wght;
public Edge(int end, int wght)
{
this.end = end;
this.wght = wght;
}
public int compareTo(Edge edge)
{
return this.wght - edge.wght;
}
}
public void run()
{
InputReader sc = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int n=sc.nextInt();
ArrayList<Integer> a[] = new ArrayList[n+1];
int i=0;
for(i=1;i<=n;i++)a[i]=new ArrayList<Integer>();
for(i=0;i<n-1;i++){
int x=sc.nextInt();
int y=sc.nextInt();
a[x].add(y);
a[y].add(x);
}
int c=0;
for(i=1;i<=n;i++){
if((a[i].size())==2){w.print("NO");c=1;break;}
}
if(c!=1)w.print("YES");
w.flush();
w.close();
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long br[200002];
long long n, x, y;
int main() {
cin >> n;
for (long long i = 1; i < n; i++) {
cin >> x >> y;
br[x]++;
br[y]++;
}
for (long long i = 1; i <= n; i++) {
if (br[i] == 2) {
cout << "NO\n";
return 0;
}
}
cout << "YES\n";
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const unsigned long long hash1 = 201326611;
const unsigned long long hash2 = 50331653;
const int N = 200000 + 10;
const int M = 20;
vector<int> g[N];
int n, vis[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
int u, v;
for (int i = 1; i < n; i++) {
cin >> u >> v;
vis[u]++;
vis[v]++;
}
int flag = 0;
for (int i = 1; i <= n; i++) {
if (vis[i] == 2) flag = 1;
}
if (flag)
cout << "NO" << endl;
else
cout << "YES" << endl;
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.util.*;
import java.io.*;
public class Main {
static FastReader in=new FastReader();
static long n,m,a,b,ans;
String S,St;
public static void main(String [] args) {
n=in.nextInt();
int a[]=new int[(int)n+1];
for(int i=1;i<n;i++){int l=in.nextInt(),v=in.nextInt();a[l]++;a[v]++;}
for(int i=0;i<=n;i++){if(a[i]==2){System.out.println("NO");return;}}
System.out.println("YES");
}
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
class Sorting{
public static int[] bucketSort(int[] array, int bucketCount) {
if (bucketCount <= 0) throw new IllegalArgumentException("Invalid bucket count");
if (array.length <= 1) return array; //trivially sorted
int high = array[0];
int low = array[0];
for (int i = 1; i < array.length; i++) { //find the range of input elements
if (array[i] > high) high = array[i];
if (array[i] < low) low = array[i];
}
double interval = ((double)(high - low + 1))/bucketCount; //range of one bucket
ArrayList<Integer> buckets[] = new ArrayList[bucketCount];
for (int i = 0; i < bucketCount; i++) { //initialize buckets
buckets[i] = new ArrayList();
}
for (int i = 0; i < array.length; i++) { //partition the input array
buckets[(int)((array[i] - low)/interval)].add(array[i]);
}
int pointer = 0;
for (int i = 0; i < buckets.length; i++) {
Collections.sort(buckets[i]); //mergeSort
for (int j = 0; j < buckets[i].size(); j++) { //merge the buckets
array[pointer] = buckets[i].get(j);
pointer++;
}
}
return array;
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.util.Scanner;
public class Code {
public static int[] arr = new int[200005], sum = new int[100005];
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for(int i=0;i<=n;i++)arr[i]=0;
for(int i=1;i<n;i++) {
int a = scanner.nextInt(), b = scanner.nextInt();
arr[a]++;
arr[b]++;
}
for(int i=1;i<=n;i++) {
if(arr[i]==2) {
System.out.printf("NO");
return;
}
}
System.out.printf("YES");
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class Codeforces {
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) throws Exception{
FastReader input = new FastReader();
int n = input.nextInt();
int[] fok = new int[n];
for (int i = 0; i < n - 1; i++) {fok[input.nextInt() - 1]++; fok[input.nextInt() - 1]++;}
boolean joe = true;
for (int i = 0; i < n; i++) if (fok[i] == 2) joe = false;
System.out.println(joe ? "YES" : "NO");
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python3
|
n = int(input())
l = [0] * n
for i in range(n - 1):
u , v = map(int , input().split())
l[u - 1] += 1
l[v - 1] += 1
for i in l:
if i == 2:
print("NO")
exit()
print("YES")
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> adj[200005];
long long c = 0;
bool vis[200005];
vector<long long> lf;
void dfs(long long v) {
if (vis[v]) {
return;
}
vis[v] = true;
if (adj[v].size() == 1) {
c++;
lf.push_back(v);
}
for (auto u : adj[v]) {
dfs(u);
}
}
int main() {
long long n;
cin >> n;
long long x, y;
for (int i = 0; i < n - 1; i++) {
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(x);
for (int i = 1; i <= n; i++) {
if (adj[i].size() == 2) {
cout << "NO";
return 0;
}
}
cout << "YES";
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class D1 {
public void solve() throws IOException {
int n = nextInt();
ArrayList<Integer>[] g = new ArrayList[n];
for (int i = 0; i < n; i++) {
g[i] = new ArrayList<>();
}
for (int i = 0; i < n - 1; i++) {
int a = nextInt() - 1;
int b = nextInt() - 1;
g[a].add(b);
g[b].add(a);
}
boolean isGood = true;
for (int i = 0; i < n; i++) {
if (g[i].size() == 2) {
isGood = false;
break;
}
}
if (isGood) {
out.print("YES");
} else {
out.print("NO");
}
}
public void run() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
BufferedReader br;
StringTokenizer in;
PrintWriter out;
public String nextToken() throws IOException {
while (in == null || !in.hasMoreTokens()) {
in = new StringTokenizer(br.readLine());
}
return in.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public int[] nextArr(int n) throws IOException {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nextInt();
}
return res;
}
public static void main(String[] args) throws IOException {
Locale.setDefault(Locale.US);
new D1().run();
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n-1;i++)
{
int x=sc.nextInt();
int y=sc.nextInt();
arr[x-1]++;
arr[y-1]++;
}
int ans=0;
for(int i=0;i<n;i++)
if(arr[i]==2)
ans=-1;
if(ans==-1)
System.out.println("NO");
else
System.out.println("YES");
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python3
|
from sys import stdin, stdout, exit
n = int(input())
graph = [[] for i in range(n)]
for i in range(n-1):
u, v = map(int, stdin.readline().split())
graph[u-1].append(v-1)
graph[v-1].append(u-1)
for i in range(n):
if len(graph[i]) == 2:
print("NO")
exit()
print("YES")
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.util.*;
import java.io.*;
public class Main {
static final int MOD_PRIME = 1000000007;
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
int i = 0;
int t = 1;
// t = in.nextInt();
for (; i < t; i++)
solver.solve(i, in, out);
out.close();
}
static class Task {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
HashMap<Integer , ArrayList<Integer>> adj = new HashMap<Integer, ArrayList<Integer>>();
for(int i = 0; i < n - 1; i++) {
int u = in.nextInt();
int v = in.nextInt();
if(!adj.containsKey(u)) adj.put(u, new ArrayList<Integer>());
if(!adj.containsKey(v)) adj.put(v, new ArrayList<Integer>());
adj.get(u).add(v);
adj.get(v).add(u);
}
long leaf = 0;
boolean twoflag = false;
for(int k : adj.keySet()) {
if(adj.get(k).size() == 1) leaf++;
if(adj.get(k).size() == 2) twoflag = true;
}
if(n - 1 > (leaf*(leaf - 1))/2 || twoflag) {
out.println("NO");
}else {
out.println("YES");
}
}
}
// template code
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static long modexp(long a, long b, long p) {
// returns a to the power b mod p by modular exponentiation
long res = 1;
long mult = a % p;
while (b > 0) {
if (b % 2 == 1) {
res = (res * mult) % p;
}
b /= 2;
mult = (mult * mult) % p;
}
return res;
}
static double log(double arg, double base) {
// returns log of a base b, contains floating point errors, dont use for exact
// calculations.
if (base < 0 || base == 1) {
throw new ArithmeticException("base cant be 1 or negative");
}
if (arg < 0) {
throw new ArithmeticException("log of negative number undefined");
}
return Math.log10(arg) / Math.log10(base);
}
static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
// scope for improvement
static ArrayList<Integer> sieveOfEratosthenes(int n) {
boolean[] check = new boolean[n + 1];
ArrayList<Integer> prime = new ArrayList<Integer>();
for (long i = 2; i <= n; i++) {
if (!check[(int) i]) {
prime.add((int) i);
for (long j = i * i; j <= n; j += i) {
check[(int) j] = true;
}
}
}
return prime;
}
static int modInverse(int a, int n) {
// returns inverse of a mod n by extended euclidean algorithm
int t = 0;
int newt = 1;
int r = n;
int newr = a;
int quotient;
int tempr, tempt;
while (newr != 0) {
quotient = r / newr;
tempt = newt;
tempr = newr;
newr = r - quotient * tempr;
newt = t - quotient * tempt;
t = tempt;
r = tempr;
}
if (r > 1) {
throw new ArithmeticException("inverse of " + a + " mod " + n + " does not exist");
} else {
if (t < 0) {
t += n;
}
return t;
}
}
static long primeModInverse(long a, long n) {
// returns inverse of a mod n by mod exponentiation, use only if n is prime
return modexp(a, n - 2, n);
}
static void dfs(HashMap<Integer, ArrayList<Integer>> adj, Set<Integer> ans, Set<Integer> open,
HashMap<String, Integer> edge, boolean[] vis, int i) {
vis[i] = true;
open.add(i);
if (adj.get(i) != null) {
for (int k : adj.get(i)) {
if (!vis[k]) {
dfs(adj, ans, open, edge, vis, k);
} else if (open.contains(k)) {
ans.add(edge.get(String.valueOf(i) + " " + String.valueOf(k)));
}
}
}
open.remove(i);
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, u, v;
cin >> n;
vector<vector<int>> g(n);
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
g[u - 1].push_back(v - 1);
g[v - 1].push_back(u - 1);
}
for (int i = 0; i < n; i++) {
if (g[i].size() == 2) {
cout << "NO";
return 0;
}
}
cout << "YES";
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.util.LinkedList;
import java.util.Scanner;
public class D {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
LinkedList[] tree = new LinkedList[N];
for (int i = 0; i < tree.length; i++) tree[i] = new LinkedList();
while (--N > 0) {
int u = in.nextInt(), v = in.nextInt();
tree[u-1].add(v-1);
tree[v-1].add(u-1);
}
boolean solutionExists = true;
for (int i = 0; i < tree.length; i++) {
if (tree[i].size() == 2) {
solutionExists = false;
break;
}
}
System.out.println(solutionExists ? "YES" : "NO");
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 1;
int d[MAX];
void solve() {
int n;
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
d[u]++;
d[v]++;
}
for (int i = 1; i <= n; ++i) {
if (d[i] == 2) {
cout << "NO\n";
return;
}
}
cout << "YES\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
vector<int> a[100010];
int main() {
int n, m;
scanf("%d", &n);
int x, y;
for (int i = 0; i < n - 1; i++) {
scanf("%d%d", &x, &y);
a[x].push_back(y);
a[y].push_back(x);
}
if (n == 2) {
printf("YES");
return 0;
}
int f = 1;
for (int i = 1; i <= n; i++) {
if (a[i].size() == 2) {
f = 0;
break;
}
}
if (f)
printf("YES");
else
printf("NO");
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class D1 {
private void solve() {
Map<Integer,Integer> map = new HashMap<>();
int N = IO.nextInt();
for(int i=0; i<N-1; i++)
{
int a = IO.nextInt();
int b = IO.nextInt();
map.put(a, 1 + map.getOrDefault(a, 0));
map.put(b, 1 + map.getOrDefault(b, 0));
}
System.out.println(map.containsValue(2) ? "NO" : "YES");
}
//*************
//*************
//TEMPLATE CODE
static long MOD = 0;
public static void main(String[] args) {
D1 instance = new D1();
int testcases = 1;//IO.nextInt();
while(testcases-->0)
instance.solve();
}
static class IO {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer st;
static String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
static int nextInt() {
return Integer.parseInt(next());
}
static long nextLong() {
return Long.parseLong(next());
}
static double nextDouble() {
return Double.parseDouble(next());
}
static int[] nextIntArray(int size) {
int a[] = new int[size];
for (int i = 0; i < size; i++)
a[i] = nextInt();
return a;
}
static long[] nextLongArray(int size) {
long a[] = new long[size];
for (int i = 0; i < size; i++)
a[i] = nextLong();
return a;
}
static double[] nextDoubleArray(int size) {
double a[] = new double[size];
for (int i = 0; i < size; i++)
a[i] = nextDouble();
return a;
}
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int O = 1e6;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 5;
const double PI = acos(-1.0);
const double E = 2.718281828459;
const double eps = 1e-8;
struct dd {
int to, val, next;
} e[maxn];
int head[maxn], cnt = 0;
void add(int u, int v) {
e[cnt] = {v, 0, head[u]};
head[u] = cnt++;
}
int main() {
memset(head, -1, sizeof(head));
int n;
scanf("%d", &n);
int num[maxn];
memset(num, 0, sizeof(num));
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf("%d%d", &u, &v);
num[u]++;
num[v]++;
}
bool flag = true;
for (int i = 1; i <= n; i++) {
if (num[i] == 2) flag = false;
}
flag ? printf("YES\n") : printf("NO\n");
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.lang.*;
import java.util.*;
import java.io.*;
public class Main {
void solve() {
int n=ni();
g=new ArrayList[n+1];
for(int i=1;i<=n;i++) g[i]=new ArrayList<>();
int deg[]=new int[n+1];
for(int i=1;i<n;i++){
int u=ni(),v=ni();
deg[u]++;
deg[v]++;
g[u].add(v); g[v].add(u);
}
if(n==2){
pw.println("YES");
return;
}
int root=-1;
for(int i=1;i<=n;i++) if(deg[i]!=1) root=i;
dfs(root,-1);
pw.println(ans==1?"YES":"NO");
}
ArrayList<Integer> g[];
int ans=1;
void dfs(int v,int pr){
for(int u : g[v]){
if(u!=pr){
dfs(u,v);
if(g[u].size()!=2 && g[v].size()>2){
}else {
ans=0;
}
}
}
}
long M = (long)1e9+7;
// END
PrintWriter pw;
StringTokenizer st;
BufferedReader br;
void run() throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
pw.flush();
}
public static void main(String[] args) throws Exception {
new Main().run();
}
String ns() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() throws Exception {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
throw new Exception(e.toString());
}
return str;
}
int ni() {
return Integer.parseInt(ns());
}
long nl() {
return Long.parseLong(ns());
}
double nd() {
return Double.parseDouble(ns());
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
//package test;
import java.util.ArrayList;
import java.util.Scanner;
public class starter {
public static void main ( String args[] )
{
Scanner conio = new Scanner ( System.in ) ;
int size = conio.nextInt() , node1 , node2 ;
ArrayList<Integer> holder [] = new ArrayList [size+1] ;
while ( conio.hasNextInt() )
{
node1 = conio.nextInt() ;
node2 = conio.nextInt() ;
if ( holder[node1] == null )
holder[node1] = new ArrayList<>() ;
if ( holder[node2] == null )
holder[node2] = new ArrayList<>() ;
holder[node1].add(node2) ;
holder[node2].add(node1) ;
}
boolean ans = false ;
for ( int i = 0 ; i <= size ; ++i )
if ( holder[i] != null && holder[i].size() == 2 )
{
ans = true ;
break ;
}
System.out.println( ans ? "NO" : "YES" ) ;
conio.close();
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> vin(long long n) {
vector<long long> a(n);
for (long long i = 0; i < n; i += 1) cin >> a[i];
return a;
}
long long intin() {
long long n;
cin >> n;
return n;
}
char charin() {
char a;
cin >> a;
return a;
}
string strin() {
string s;
cin >> s;
return s;
}
long long factorial(long long n) {
return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, u, v;
cin >> n;
vector<long long> counter(n + 1, 0);
for (long long i = 0; i < n - 1; i += 1) {
cin >> u >> v;
counter[u]++;
counter[v]++;
}
for (auto i : counter) {
if (i == 2) {
cout << "NO\n";
return 0;
}
}
cout << "YES\n";
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
// https://codeforces.com/contest/1189/problem/D1
// 只要有一个顶点的度为 2,那么就一定有无法满足的情况,输出 NO
public class Main {
static List<List<Integer>> node = new ArrayList<List<Integer>>();
static void addList(int from, int to) {
node.get(from).add(to);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 0; i < n+1; i++) {
node.add(new LinkedList<Integer>());
}
for (int i = 0; i < n-1; i++) {
int a = scanner.nextInt();
int b = scanner.nextInt();
addList(a, b);
addList(b, a);
}
scanner.close();
// 点的标号是从 1 开始的
for (int i = 1; i < n+1; i++) {
if (node.get(i).size() == 2) {
System.out.println("NO");
return;
}
}
System.out.println("YES");
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
//I AM THE CREED
/* //I AM THE CREED
/* package codechef; // don't place package name! */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.*;
import java.awt.Point;
public class Main{
public static void main(String[] args) throws IOException
{
FastScanner input = new FastScanner();
int n=input.nextInt();
int[] deg=new int[n];
for(int i=0;i<n-1;i++){
deg[input.nextInt()-1]++;
deg[input.nextInt()-1]++;
}
boolean possible=true;
for(int i=0;i<n;i++){
if(deg[i]==2){
possible=false;
}
}
System.out.println(possible?"YES":"NO");
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python3
|
from collections import Counter
n = int(input())
x = []
for _ in range(n-1):
x.extend(input().split())
print('NO' if 2 in Counter(x).values() else 'YES')
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n;
cin >> n;
vector<vector<long long>> v(n);
for (long long i = 0; i < n - 1; i++) {
long long a, b;
cin >> a >> b;
v[a - 1].push_back(b - 1);
v[b - 1].push_back(a - 1);
}
for (long long i = 0; i < n; i++) {
if (v[i].size() == 2) {
cout << "NO";
return 0;
}
}
cout << "YES";
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python3
|
import sys, os
from io import BytesIO, IOBase
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
stdin, stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
mod = pow(10, 9) + 7
mod2 = 998244353
def inp(): return stdin.readline().strip()
def iinp(): return int(inp())
def out(var, end="\n"): stdout.write(str(var)+"\n")
def outa(*var, end="\n"): stdout.write(' '.join(map(str, var)) + end)
def lmp(): return list(mp())
def mp(): return map(int, inp().split())
def smp(): return map(str, inp().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(m, val) for j in range(n)]
def remadd(x, y): return 1 if x%y else 0
def ceil(a,b): return (a+b-1)//b
S1 = 'abcdefghijklmnopqrstuvwxyz'
S2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def isprime(x):
if x<=1: return False
if x in (2, 3): return True
if x%2 == 0: return False
for i in range(3, int(sqrt(x))+1, 2):
if x%i == 0: return False
return True
n = iinp()
edges = l1d(n+1)
for i in range(n-1):
x, y = mp()
edges[x] += 1
edges[y] += 1
flg = True
for i in edges:
if i==2:
flg = False
break
print("YES" if flg else "NO")
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long n, m, x, y, d[100005];
vector<long long> a[100005];
void solve() {
cin >> n;
m = n - 1;
while (m--) {
cin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
d[x]++, d[y]++;
}
for (__typeof((n + 1)) i = (1); i < (n + 1); i++) {
if (d[i] == 2) {
cout << "NO"
<< "\n";
return;
};
}
{
cout << "YES"
<< "\n";
return;
};
}
void prep() {}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
prep();
cout << fixed << setprecision(12);
while (t--) solve();
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader sc = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(1, sc, out);
out.close();
}
static class Task {
public void solve(int testNumber, InputReader sc, PrintWriter out) {
int n=sc.nextInt();
ArrayList<Integer>[] map=new ArrayList[n+1];
for(int i=1;i<=n;i++)
map[i]=new ArrayList<Integer>();
for(int i=0;i<n-1;i++) {
int u=sc.nextInt();
int v=sc.nextInt();
map[u].add(v);
map[v].add(u);
}
boolean[] jud=new boolean[n+1];
for(int i=1;i<=n;i++) {
if(map[i].size()==1)
jud[i]=true;
}
for(int i=1;i<=n;i++) {
if(!jud[i]&&map[i].size()==2) {
out.println("NO");
return ;
}
}
out.println("YES");
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class CF1189D1 {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws Exception {
int N = Integer.parseInt(reader.readLine());
StringTokenizer st;
List<List<Integer>> adj = new ArrayList<>();
for (int i = 0; i <= N; i++) {
adj.add(new ArrayList<>());
}
for (int n = 1; n < N; n++) {
st = new StringTokenizer(reader.readLine());
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
adj.get(u).add(v);
adj.get(v).add(u);
}
boolean noDegree2 = true;
for (int i = 1; i <= N; i++) {
if (adj.get(i).size() == 2) {
noDegree2 = false;
break;
}
}
writer.write((noDegree2 ? "YES" : "NO"));
writer.newLine();
writer.flush();
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
//segement tree
//balls circular
//DFS/BFS/recur
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n=in.nextInt();
int[][] arr=new int[n][2];
HashMap<Integer,Integer> lol = new HashMap<Integer,Integer>();
for(int i=0;i<n-1;i++){
arr[i][0]=in.nextInt();
arr[i][1]=in.nextInt();
if(lol.containsKey(arr[i][0]))
lol.put(arr[i][0],lol.get(arr[i][0])+1);
else lol.put(arr[i][0],1);
if(lol.containsKey(arr[i][1]))
lol.put(arr[i][1],lol.get(arr[i][1])+1);
else lol.put(arr[i][1],1);
}
if(!lol.containsValue(2)) System.out.println("YES");
else System.out.println("NO");
}
// PriorityQueue<Integer> p = new PriorityQueue<Integer>(5,new lolCompare());
// TreeMap<Integer,Integer> lol = new TreeMap<Integer,Integer>();
// LinkedList<Integer> l = new LinkedList<Integer>();
// while(i<10)
// { i++;
// int n=in.nextInt();
// p.add(n);
// lol.put(i+n,n);
// l.add(n);
// }
// while (!p.isEmpty()) {
// System.out.println(p.poll());
// }
// System.out.println(lol.entrySet());
// // int x = lol.size();
// // while (x > 0) {
// // System.out.println(lol.get(x));
// // x--;
// // }
// for(Map.Entry<Integer,Integer> mpa : lol.entrySet()){
// System.out.println(mpa.getKey()+" "+mpa.getValue());
// }
// Iterator<Integer> i1 = l.iterator();
// //power(x,y,p);
// while(i1.hasNext()){
// System.out.println(i1.next());
// }
// public static void power(int x,int y,int p){
// System.out.println(Math.pow(x,y));
// int ans=1;
// x=x%p;
// while(y>0){
// if(y%2 == 1)
// ans=(ans*x) % p ;
// y=y>>1;
// x=(x*x)%p;
// }
// System.out.println(ans);
// }
}
class lolCompare implements Comparator<Integer> {
public int compare(Integer a,Integer b){
if(a>b)return 1;
else if(a<b) return -1;
else return 0;
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, l, r, cnt = 0;
cin >> n;
vector<long long> v[n + 1];
bool ok = true;
for (long long i = 0; i < n - 1; ++i) {
cin >> l >> r;
v[l].push_back(r);
v[r].push_back(l);
}
for (long long i = 1; i <= n; ++i) {
if (v[i].size() == 2) ok = false;
}
cout << ((ok) ? "YES\n" : "NO\n");
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class Main {
static InputReader in = new InputReader(System.in);
static PrintWriter out = new PrintWriter(System.out);
static long oo = 1000000000000L;
public static void main(String[] args) throws IOException {
int n = in.nextInt();
ArrayList<Integer>[] adj = new ArrayList[n];
for(int i = 0; i < n; ++i)
adj[i] = new ArrayList<>();
for(int i = 0; i < n-1; ++i) {
int u = in.nextInt() - 1, v = in.nextInt() - 1;
adj[u].add(v);
adj[v].add(u);
}
for(int i = 0; i < n; ++i) {
if(adj[i].size() == 2) {
System.out.println("NO");
return;
}
}
System.out.println("YES");
out.close();
}
static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
static boolean nextPermutation(int[] a) {
for(int i = a.length - 2; i >= 0; --i) {
if(a[i] < a[i+1]) {
for(int j = a.length - 1; ; --j) {
if(a[i] < a[j]) {
int t = a[i];
a[i] = a[j];
a[j] = t;
for(i++, j = a.length - 1; i < j; ++i, --j) {
t = a[i];
a[i] = a[j];
a[j] = t;
}
return true;
}
}
}
}
return false;
}
static void shuffle(int[] a) {
Random r = new Random();
for(int i = a.length - 1; i > 0; --i) {
int si = r.nextInt(i);
int t = a[si];
a[si] = a[i];
a[i] = t;
}
}
static void shuffle(long[] a) {
Random r = new Random();
for(int i = a.length - 1; i > 0; --i) {
int si = r.nextInt(i);
long t = a[si];
a[si] = a[i];
a[i] = t;
}
}
static int lower_bound(int[] a, int n, int k) {
int s = 0;
int e = n;
int m;
while (e - s > 0) {
m = (s + e) / 2;
if (a[m] < k)
s = m + 1;
else
e = m;
}
return e;
}
static int lower_bound(long[] a, int n, long k) {
int s = 0;
int e = n;
int m;
while (e - s > 0) {
m = (s + e) / 2;
if (a[m] < k)
s = m + 1;
else
e = m;
}
return e;
}
static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
static class Pair implements Comparable<Pair> {
int first, second;
public Pair(int first, int second) {
super();
this.first = first;
this.second = second;
}
@Override
public int compareTo(Pair o) {
return this.first != o.first ? this.first - o.first : this.second - o.second;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + first;
result = prime * result + second;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pair other = (Pair) obj;
if (first != other.first)
return false;
if (second != other.second)
return false;
return true;
}
}
}
class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
var n = +readline();
var arr = [];
for (var i = 0; i < n; i++) {
arr[i] = 0;
}
for (var i = 1; i < n; i++) {
var point = readline().split(' ').map(item => +item - 1);
arr[point[0]]++;
arr[point[1]]++;
}
print(arr.find(item => item === 2) ? 'NO' : 'YES');
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
vector<int> AL[100005];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf("%d", &u);
scanf("%d", &v);
AL[u].push_back(v);
AL[v].push_back(u);
}
long long int l = 0;
int fl = 0;
for (int i = 1; i <= n; i++) {
if (AL[i].size() == 2) {
fl = 1;
}
}
if (fl == 0) {
printf("YES\n");
} else {
printf("NO\n");
}
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.util.*;
import java.math.*;
// **** D1. Add on a Tree ****
public class D1 {
static char [] in = new char [1000000];
public static void main (String [] arg) throws Throwable {
int n = nextInt();
AL [] AL = new AL[n+1];
for (int i = 0; i<n-1; ++i) {
int N1 = nextInt();
int N2 = nextInt();
if (AL[N1] == null) AL[N1] = new AL();
if (AL[N2] == null) AL[N2] = new AL();
AL[N1].add(N2);
AL[N2].add(N1);
}
boolean cancan = true;
for (int i = 1; i<=n; ++i) if (AL[i].size() == 2) cancan = false;
System.out.println(cancan ? "YES" : "NO");
}
/************** HELPER CLASSES ***************/
//static class HS extends HashSet<Integer>{public HS(){super();}public HS(int a){super(a);}};
static class AL extends ArrayList<Integer>{public AL(){super();}public AL(int a){super (a);}};
static class Pair implements Comparable<Pair> {int i,j;long L; public Pair(int xx, int yy, long LL){i=xx;j=yy;L=LL;} public int compareTo(Pair p) {
return (this.L < p.L) ? -1 : ((this.L == p.L && this.i < p.i) ? -1 : 1);}
}
/************** FAST IO CODE FOLLOWS *****************/
public static long nextLong() throws Throwable {
long i = System.in.read();boolean neg = false;while (i < 33) i = System.in.read();if (i == 45) {neg=true;i=48;}i = i - 48;
int j = System.in.read();while (j > 32) {i*=10;i+=j-48;j = System.in.read();}return (neg) ? -i : i;
}
public static int nextInt() throws Throwable {return (int)nextLong();}
public static String next() throws Throwable {
int i = 0; while (i < 33 && i != -1) i = System.in.read(); int cptr = 0; while (i >= 33) { in[cptr++] = (char)i; i = System.in.read();}
return new String(in, 0,cptr);
}
/**** LIBRARIES ****/
public static long gcdL(long a, long b) {while (b != 0) {long tmp = b;b = (a % b);a = tmp;}return a;}
public static int gcd(int a, int b) {while (b != 0) {int tmp = b;b = (a % b);a = tmp;}return a;}
public static int[] sieve(int LIM) {
int i,count = 0;
boolean [] b = new boolean [LIM];
for (i = 2;i<LIM; ++i) if (!b[i]) {count++; for (int j = i<<1; j<LIM; j+=i) b[j] = true;}
int [] primes = new int[count];
for (i = 2,count=0;i<LIM;++i) if (!b[i]) primes[count++] = i;
return primes;
}
public static int[] numPrimeFactors(int LIM) {
int i,count = 0;
int [] b = new int [LIM];
for (i = 2;i<LIM; ++i) if (b[i] == 0) {count++; for (int j = i; j<LIM; j+=i) b[j]++;}
return b;
}
public static StringBuilder stringFromArray(int [] a) {
StringBuilder b = new StringBuilder(9*a.length);
for (int i = 0; i<a.length; ++i) {
if (i != 0) b = b.append(' ');
b = b.append(a[i]);
}
return b;
}
}
/* Full Problem Text:
Note that this is the first problem of the two similar problems.
You can hack this problem only if you solve both problems.
You are given a tree with n nodes.
In the beginning, 0 is written on all edges.
In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1.
Simple path is a path that doesn't contain any node twice.
*/
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int g[100010];
int f;
int main() {
int n, i, j, ok = 0, x, y;
scanf("%d", &n);
for (i = 1; i < n; i++) {
scanf("%d%d", &x, &y);
g[x]++;
g[y]++;
}
for (i = 1; i <= n; i++) {
if (g[i] == 1) f++;
if (g[i] == 2) ok = 1;
}
f = (f * (f - 1)) / 2;
if (ok)
printf("NO");
else
printf("YES");
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> adj[n];
int u, v;
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
u--;
v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 0; i < n; i++) {
if (adj[i].size() == 2) {
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int n, root, Over;
vector<int> P[MAXN];
int main() {
scanf("%d", &n);
for (int i = 1, u, v; i < n; i++) {
scanf("%d%d", &u, &v);
P[u].push_back(v);
P[v].push_back(u);
}
for (int i = 1; i <= n; i++)
if (P[i].size() == 2) {
printf("NO\n");
return 0;
}
printf("YES\n");
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long in[100005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, x, y;
cin >> n;
for (long long i = 0; i < n - 1; i++) {
cin >> x >> y;
in[x]++;
in[y]++;
}
long long cnt = 0;
bool first = 1;
for (long long i = 1; i < n + 1; i++) {
if (in[i] == 1) cnt++;
if (in[i] == 2) {
first = 0;
break;
}
}
if (first == 0) {
cout << "NO"
<< "\n";
return 0;
}
if ((cnt * (cnt - 1)) / 2 >= n - 1) {
cout << "YES"
<< "\n";
} else {
cout << "NO"
<< "\n";
}
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, u, v, z = 0, f[100001];
int main() {
for (int i = 1; i <= 10000; i++) z++;
for (int i = 1; i <= 100; i++) z--;
cin >> n;
for (int i = 1; i < n; i++) {
cin >> u >> v;
f[u]++;
f[v]++;
}
for (int i = 1; i <= n; i++)
if (f[i] != 1 && f[i] <= 2) {
cout << "NO";
return 0;
}
cout << "YES";
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python3
|
n=int(input())
dic={}
for x in range(n-1):
a,b=list(map(int,input().split()))
if a in dic:
dic[a]+=1
else:
dic[a]=1
if b in dic:
dic[b]+=1
else:
dic[b]=1
flag=5
for it in dic:
if dic[it]==2:
flag=6
break
if flag==6:
print("NO")
else:
print("YES")
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python3
|
n = int(input())
deg = [0 for i in range(n+1)]
for i in range(n-1):
a,b = [int(j) for j in input().split(" ")]
deg[a] += 1
deg[b] += 1
ans = "YES"
for i in range(n+1):
if deg[i] == 2:
ans = "NO"
break
print(ans)
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int n, x, y;
vector<int> g[100010];
int main() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
for (int i = 1; i <= n; i++) {
if (g[i].size() == 2) {
puts("NO");
return 0;
}
}
puts("YES");
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long int n;
vector<long long int> G[200020];
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
for (int i = 1; i <= n; i++) {
if (G[i].size() == 2) return cout << "NO", 0;
}
cout << "YES";
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
int n = sc.nextInt();
List<Integer>[] tree = new List[n + 1];
boolean[] leafs = new boolean[n + 1];
for (int i = 1; i <= n; i++) {
tree[i] = new ArrayList();
}
for (int i = 0; i < n - 1; i++) {
int v = sc.nextInt();
int u = sc.nextInt();
tree[v].add(u);
tree[u].add(v);
}
for (int i = 1; i <= n; i++) {
if (tree[i].size() == 1) {
leafs[i] = true;
}
}
for (int i = 1; i <= n; i++) {
if (!leafs[i]) {
if (tree[i].size() == 2) {
System.out.println("NO");
return;
}
}
}
System.out.println("YES");
}
static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class R4 {
public static void main(String[] Args){
FastReader scan=new FastReader();
int n=scan.nextInt();
node[] arr=new node[n+1];
for (int i = 1; i <=n ; i++) {
arr[i]=new node(i+1);
}
for(int i=0;i<n-1;i++){
int u=scan.nextInt();
int v=scan.nextInt();
arr[u].neighs.add(v);
arr[v].neighs.add(u);
}
for (int i = 1; i <=n ; i++) {
if(arr[i].neighs.size()==2){
System.out.println("NO");
return;
}
}
System.out.println("YES");
}
static class node{
int v;
ArrayList<Integer> neighs;
node(int v){
this.v=v;
neighs=new ArrayList<>();
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python2
|
from collections import defaultdict
from sys import stdin
raw_input = stdin.readline
n=int(raw_input())
d=defaultdict(list)
for i in range(n-1):
u,v=map(int,raw_input().split())
d[u].append(v)
d[v].append(u)
if n==2:
print 'YES'
exit()
for i in range(1,n+1):
if len(d[i])==1:
continue
else:
if len(d[i])<=2:
print 'NO'
exit()
print 'YES'
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.StringTokenizer;
public class D {
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
BigInteger nextBigInteger() {
try {
return new BigInteger(nextLine());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
}
public static void main(String[] args) {
FastReader fr = new FastReader();
FastWriter fw = new FastWriter();
int n = fr.nextInt();
ArrayList<ArrayList<Integer>> g = new ArrayList<>();
for (int i = 0; i < n; i++) g.add(new ArrayList<>());
for (int i = 0; i < n - 1; i++) {
int u = fr.nextInt() - 1;
int v = fr.nextInt() - 1;
g.get(u).add(v);
g.get(v).add(u);
}
HeapMover m = new HeapMover();
m.flag = true;
boolean visited[] = new boolean[n];
helper(g, 0, -1, m, visited);
if (m.flag) System.out.println("YES");
else System.out.println("NO");
}
public static class HeapMover {
boolean flag;
}
public static void helper(ArrayList<ArrayList<Integer>> g, int c, int p, HeapMover m, boolean[] visited) {
visited[c] = true;
if (p != -1) {
// if (g.get(c).size() == 1) {
if (g.get(p).size() == 2) m.flag = false;
// }
}
for(int ch : g.get(c)){
if(!visited[ch]){
helper(g,ch,c,m,visited);
}
}
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int dirx[] = {1, -1, 0, 0}, diry[] = {0, 0, 1, -1};
long long bigmod(long long x, long long p) {
long long res = 1;
while (p) {
if (p & 1) res = (res * x) % 998244353;
x = (x * x) % 998244353;
p >>= 1;
}
return res;
}
int32_t main() {
ios_base::sync_with_stdio(false);
int n, m;
cin >> n;
m = n - 1;
vector<int> cnt(n);
while (m--) {
int u, v;
cin >> u >> v;
cnt[u - 1]++;
cnt[v - 1]++;
}
int i = 0;
for (; i < n; i++)
if (cnt[i] == 2) break;
cout << (i < n ? "NO" : "YES\n");
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.util.*;
import java.io.*;
public class d1
{
int n;
int[] deg;
ArrayList<Integer>[] G;
d1(Scanner in, PrintWriter out)
{
n = in.nextInt();
G = new ArrayList[n];
for (int i = 0; i < n; i++)
G[i] = new ArrayList<>();
deg = new int[n];
for (int i = 0; i < n-1; i++)
{
int u = in.nextInt()-1;
int v = in.nextInt()-1;
G[u].add(v);
G[v].add(u);
deg[u]++;
deg[v]++;
}
boolean fail = false;
for (int i = 0; i < n; i++)
if (deg[i] == 2)
fail = true;
if (fail) out.println("NO");
else out.println("YES");
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
new d1(in, out);
out.close();
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python2
|
from sys import stdin,stdout
I = lambda : map(int,stdin.readline().split())
P = lambda x: stdout.write(str(x)+" ")
n = input()
g=[]
for i in range(n):
g.append([])
l = [0]*n
vis = [0]*n
for i in range(n-1):
u,v = I()
u,v = u-1,v-1
g[u].append(v)
l[u]+=1
g[u].append(u)
l[v]+=1
if n == 2:
print "YES"
elif l.count(1)<=2:
print "NO"
else:
imp = 0
for i in range(n):
if l[i]==2:
imp = 1
break
if imp:
print "NO"
else:
print "YES"
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int g[100005];
int main() {
int n;
memset(g, 0, sizeof(g));
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
g[u]++;
g[v]++;
}
for (int i = 1; i <= n; i++) {
if (g[i] == 2) {
cout << "NO";
return 0;
}
}
cout << "YES";
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.util.ArrayList;
import java.util.Scanner;
public class codeForcesD1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
ArrayList[] array=new ArrayList[n];
for(int i=0;i<n;i++) {
array[i]=new ArrayList<Integer>();
}
for(int i=0;i<n-1;i++) {
int t1=sc.nextInt()-1;
int t2=sc.nextInt()-1;
array[t1].add(t2);
array[t2].add(t1);
}
{
boolean answer=true;
for(int i=0;i<n;i++) {
int temp=array[i].size();
if(temp==2) {
answer=false;
}
}
if(answer) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
sc.close();
}
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
;
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
const long long INFLL = 0x3f3f3f3f3f3f3f3f;
struct EDGE {
int v, nxt;
} edge[maxn << 1];
int head[maxn], tot;
void add_edge(int u, int v) {
edge[tot].v = v, edge[tot].nxt = head[u], head[u] = tot++;
}
int degree[maxn];
int main() {
memset(head, -1, sizeof(head));
tot = 0;
int n;
scanf("%d", &n);
for (int i = 1, u, v; i < n; i++) {
scanf("%d%d", &u, &v);
degree[u]++;
degree[v]++;
}
bool flag = true;
for (int i = 1; i <= n; i++) {
if (degree[i] == 2) {
flag = false;
}
}
if (flag) {
printf("YES\n");
} else {
printf("NO\n");
}
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python3
|
n = int(input())
neigh = [[] for _ in range(n)]
for i in range(n-1):
u, v = map(int, input().split())
u -= 1
v -= 1
neigh[u].append(v)
neigh[v].append(u)
no = False
for i in range(n):
if len(neigh[i]) == 2:
no = True
if no:
print('NO')
else:
print('YES')
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
const int BASE = 1e9 + 7;
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const int N = 1e5 + 1;
vector<int> a[N];
int pa[N], n;
int F[N];
void DFS(int p, int u) {
bool Check = true;
for (__typeof(a[u].begin()) it = a[u].begin(); it != a[u].end(); it++) {
int v = *it;
if (v == p) continue;
Check = false;
DFS(u, v);
pa[v] = u;
F[u] = F[u] + F[v];
}
F[u] += Check;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
int u, v;
for (int i = 1; i < n; i++) {
cin >> u >> v;
a[u].push_back(v);
a[v].push_back(u);
}
bool Check = true;
for (int u = 1; u <= n; u++) {
for (__typeof(a[u].begin()) it = a[u].begin(); it != a[u].end(); it++) {
int v = *it;
if (a[u].size() == 1) {
if (a[v].size() == 1) continue;
if (a[v].size() < 3) {
Check = false;
}
} else if (a[v].size() == 1) {
if (a[u].size() == 1) continue;
if (a[u].size() < 3) {
Check = false;
}
} else {
if (a[v].size() < 3 || a[u].size() < 3) {
Check = false;
}
}
}
}
cout << (Check ? "YES" : "NO");
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python3
|
import sys
input = lambda: sys.stdin.readline().strip()
n = int(input())
graph = {}
for i in range(1, n+1): graph[i] = []
for i in range(1, n):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
for i in range(1, n+1):
if len(graph[i])==2:
print("NO")
break
else: print("YES")
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
std::vector<long long> edge[100045];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t, i, j, n, m, k, q, temp, x, y;
{
long long count = 0;
bool flag = 0;
long long a[200050] = {0};
string s;
cin >> n;
for (int i = 0; i < n - 1; i++) {
cin >> x >> y;
edge[x].push_back(y);
edge[y].push_back(x);
}
{
flag = true;
for (i = 1; i <= n; i++) {
if (edge[i].size() == 2) {
flag = false;
}
}
if (flag) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
}
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
bool is_prime(long long n) {
for (long long i = 2; i * i <= n; ++i) {
if (n % i == 0) {
return false;
}
}
return true;
}
vector<long long> fact(long long n) {
n = abs(n);
vector<long long> ans;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
ans.push_back(i);
ans.push_back(n / i);
}
}
return ans;
}
inline long long getPow(long long a, long long b) {
long long res = 1ll, tp = a;
while (b) {
if (b & 1ll) {
res *= tp;
}
tp *= tp;
b >>= 1ll;
}
return res;
}
long long vec_mult(long long x1, long long y1, long long x2, long long y2,
long long x3, long long y3) {
return abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1));
}
void ok() {
cout << "YES" << endl;
exit(0);
}
void no() {
cout << "NO" << endl;
exit(0);
}
inline long long nxt() {
long long x;
cin >> x;
return x;
}
const long long N = 3e5 + 5, inf = 8e18;
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
long long n = nxt();
vector<vector<long long>> g(n);
for (int i = 1; i < n; i++) {
long long t1 = nxt() - 1, t2 = nxt() - 1;
g[t1].push_back(t2);
g[t2].push_back(t1);
}
map<long long, long long> mp;
for (auto x : g) {
if (x.size() == 2) {
no();
}
}
ok();
return 0;
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, a, b;
vector<int> adj[100005];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = (int)1; i <= (int)n - 1; i++) {
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = (int)1; i <= (int)n; i++)
if (adj[i].size() == 2) {
cout << "NO";
return 0;
}
cout << "YES";
}
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python3
|
n = int(input())
node = []
for i in range(1 + n):
node.append( [] )
for q in range(n - 1):
k = list(map(int, input().split()))
node[ k[0] ].append( k[1] )
node[ k[1] ].append( k[0] )
hobe = True
for i in range(n + 1):
if(len(node[i]) == 2):
hobe = False
if hobe :
print("YES")
else :
print("NO")
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python2
|
import fileinput
def main():
I=0
N=0
A=0
for l in fileinput.input():
I+=1
if(I<2):
N=int(l)
A=[0]*N
continue
A[int(l.split(" ")[0])-1]+=1
A[int(l.split(" ")[1])-1]+=1
for i in A:
if(i==2):
print("NO")
return
print("YES")
main()
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
python3
|
#! /usr/bin/env python3
import sys
from math import factorial
class Node:
def __init__(self, num):
self.num = num
self.binds = []
# for dijkstra
self.marker = False
self.val = None
def add_bind(self, oth):
self.binds.append(oth)
def __repr__(self):
return '<{}: {}{}>'.format(
self.num,
[i.num for i in self.binds],
', \tval: {}'.format(self.val) if self.val != None else ''
)
class Graph:
def __init__(self, size):
self.size = size
self.nodes = [None] + [Node(num) for num in range(1, size+1)]
def read_input(self):
for _ in range(1, self.size):
i, j = (int(x) for x in sys.stdin.readline().split())
self.nodes[i].add_bind(self.nodes[j])
self.nodes[j].add_bind(self.nodes[i])
def __repr__(self):
return '\n'.join(str(node) for node in self.nodes[1:])
def pairs(n):
return factorial(n) // ( factorial(n-2) * 2 )
N = int(sys.stdin.readline())
g = Graph(N)
g.read_input()
#print(g)
ends = [node for node in g.nodes[1:] if len(node.binds) == 1]
#print('YES' if pairs(len(ends)) >= N-1 else 'NO')
for n in g.nodes[1:]:
if len(n.binds) == 2:
print('NO')
break
else:
print('YES')
|
1189_D1. Add on a Tree
|
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
|
{
"input": [
"2\n1 2\n",
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n2 5\n",
"6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
],
"output": [
"YES",
"NO",
"NO",
"YES"
]
}
|
{
"input": [
"50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n",
"10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n",
"5\n5 1\n5 4\n4 3\n1 2\n",
"7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n",
"3\n1 3\n2 3\n",
"60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n",
"7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n",
"20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n",
"4\n2 4\n2 3\n2 1\n",
"4\n1 4\n3 2\n1 3\n",
"3\n1 2\n1 3\n",
"5\n1 2\n1 5\n1 3\n1 4\n",
"20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n",
"20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n",
"8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n",
"5\n5 1\n5 2\n5 3\n5 4\n",
"50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n",
"20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n",
"10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n"
],
"output": [
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"NO",
"YES",
"NO",
"NO",
"YES",
"NO",
"NO",
"NO",
"YES",
"NO",
"YES",
"NO"
]
}
|
CORRECT
|
java
|
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class Main {
private static final String NO = "NO";
private static final String YES = "YES";
InputStream is;
PrintWriter out;
String INPUT = "";
private static final long MOD = 998244353;
void solve() {
int T = 1;// ni();
for (int i = 0; i < T; i++)
solve(i);
}
void solve(int nth) {
int n = ni();
int pre[] = new int[n + 1];
for (int i = 0; i < n-1; i++) {
int u = ni();
int v = ni();
pre[u]++;
pre[v]++;
}
for (int i = 1; i <= n; i++)
if (pre[i] == 2) {
out.println(NO);
return;
}
out.println(YES);
}
// a^b
long power(long a, long b) {
long x = 1, y = a;
while (b > 0) {
if (b % 2 != 0) {
x = (x * y) % MOD;
}
y = (y * y) % MOD;
b /= 2;
}
return x % MOD;
}
private long gcd(long a, long b) {
while (a != 0) {
long tmp = b % a;
b = a;
a = tmp;
}
return b;
}
void run() throws Exception {
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
if (!INPUT.isEmpty())
tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new Main().run();
}
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if (lenbuf == -1)
throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private char[] nc(int n) {
char[] ret = new char[n];
for (int i = 0; i < n; i++)
ret[i] = nc();
return ret;
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != '
// ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n) {
if (!(isSpaceChar(b)))
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++)
map[i] = ns(m);
return map;
}
private int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private Integer[] na2(int n) {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private int[][] na(int n, int m) {
int[][] a = new int[n][];
for (int i = 0; i < n; i++)
a[i] = na(m);
return a;
}
private int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private Long[] nl2(int n) {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nl();
return a;
}
private long[] nl(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nl();
return a;
}
private long[][] nl(int n, int m) {
long[][] a = new long[n][];
for (int i = 0; i < n; i++)
a[i] = nl(m);
return a;
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) {
System.out.println(Arrays.deepToString(o));
}
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.