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
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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int MAX = 262144; const int END = 200000; const int INF = 1e9 + 7; int n; int arr[100011]; int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int x, y; scanf("%d %d", &x, &y); arr[x]++; arr[y]++; } int cnt = 0; for (int i = 1; i <= n; i++) { if (arr[i] == 1) cnt++; } if (cnt * (cnt - 1) / 2 < (n - 1)) printf("NO\n"); else 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" ] }
IN-CORRECT
python3
from collections import defaultdict, deque, Counter from sys import stdin, stdout from heapq import heappush, heappop import math import io import os import math import bisect #?############################################################ def isPrime(x): for i in range(2, x): if i*i > x: break if (x % i == 0): return False return True #?############################################################ def ncr(n, r, p): num = den = 1 for i in range(r): num = (num * (n - i)) % p den = (den * (i + 1)) % p return (num * pow(den, p - 2, p)) % p #?############################################################ def primeFactors(n): l = [] while n % 2 == 0: l.append(2) n = n / 2 for i in range(3, int(math.sqrt(n))+1, 2): while n % i == 0: l.append(int(i)) n = n / i if n > 2: l.append(n) return list(set(l)) #?############################################################ def power(x, y, p): res = 1 x = x % p if (x == 0): return 0 while (y > 0): if ((y & 1) == 1): res = (res * x) % p y = y >> 1 x = (x * x) % p return res #?############################################################ def sieve(n): prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 return prime #?############################################################ def digits(n): c = 0 while (n > 0): n //= 10 c += 1 return c #?############################################################ def ceil(n, x): if (n % x == 0): return n//x return n//x+1 #?############################################################ def mapin(): return map(int, input().split()) #?############################################################ # input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline # python3 15.py<in>op n = int(input()) d = [0]*n for _ in range(n-1): u, v = mapin() d[u-1]+=1 d[v-1]-=1 if(d.count(2) == 0): 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> long long mod = 998244353; using namespace std; const long long N = 1e5 + 5; vector<long long> g[N]; long long n; void fail() { cout << "NO\n"; exit(0); } long long cnt = 1; void dfs(long long node, long long p = 0) { if (g[node].size() == 1) { return; } if (g[node].size() == 2) cnt = 0, cout << node << '\n'; ; for (long long nx : g[node]) { if (nx == p) continue; dfs(nx, node); } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; if (n == 2) { cout << "YES\n"; return 0; } if (n == 3) fail(); for (long long i = 1, u, v; i < n; i++) { cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } dfs(1); if (cnt) cout << "YES"; else cout << "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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, num, head[100010], f[100010][2]; struct node { int v, pre; } e[100010 * 2]; vector<int> G; void Add(int from, int to) { num++; e[num].v = to; e[num].pre = head[from]; head[from] = num; } void Dfs(int now, int from) { f[now][0] = 0; int falg = 0; for (int i = head[now]; i; i = e[i].pre) { int v = e[i].v; if (v == from) continue; falg = 1; Dfs(v, now); f[now][0] += f[v][0]; } if (falg == 0) { f[now][0] = 1; G.push_back(now); } } void dfs(int now, int from) { f[now][1] = f[from][0] - f[now][0] + f[from][1]; if (now == 1) f[now][1] = 0; for (int i = head[now]; i; i = e[i].pre) { int v = e[i].v; if (v == from) continue; dfs(v, now); } } bool check(int now, int from) { for (int i = head[now]; i; i = e[i].pre) { int v = e[i].v; if (v == from) continue; if (f[v][0] < 2 || f[v][1] < 2 || check(v, now) == 0) return 0; } return 1; } int main() { scanf("%d", &n); int u, v; for (int i = 1; i < n; i++) { scanf("%d%d", &u, &v); Add(u, v); Add(v, u); } Dfs(1, 0); dfs(1, 0); for (int i = 0; i < G.size(); i++) f[G[i]][0] = 100; if (check(1, 0)) printf("YES\n"); else printf("NO\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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, a, b; cin >> n; int ind[100005]; for (int i = 1; i < n; i++) { cin >> a >> b; ind[a]++; ind[b]++; } for (int i = 0; i < n; i++) { if (ind[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" ] }
IN-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 == 1) { c1++; } else c2++; } if ((c1 + c2) % 2 == 0) cout << "YES"; else cout << "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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int N; vector<long long> adj[100001]; bool yes = 1; int rt; void dfs(int x, int p) { if (p != -1) { int num = adj[p].size() - 1; if (p != rt) num--; if (num <= 0) yes = 0; } for (auto i : adj[x]) { if (i != p) dfs(i, x); } } int main() { ios_base::sync_with_stdio(false), cin.tie(0); cin >> N; for (long long i = 1; i <= long long(N - 1); ++i) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } for (long long i = 1; i <= long long(N); ++i) { if (adj[i].size() > 1) rt = i; } if (rt == 0) { cout << "YES"; return 0; } dfs(rt, -1); if (yes) cout << "YES"; else cout << "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" ] }
IN-CORRECT
java
import java.util.Scanner; import java.util.TreeSet; import java.util.LinkedList; public class D1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); LinkedList<Integer> adj[] = new LinkedList[n + 1]; for (int i = 1; i <= n; i++) { adj[i] = new LinkedList<>(); } int v, w; for (int i = 0; i < n - 1; i++) { v = sc.nextInt(); w = sc.nextInt(); adj[v].add(w); adj[w].add(v); } for (int i = 1; i <= n; i++) { if (adj[i].size() == 1) { if (adj[adj[i].peekFirst()].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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long inf = LONG_MAX; const long long arr = 1000000; bool comp(pair<long long, long long> a, pair<long long, long long> b) { if (a.first == b.first) return a.second < b.second; else return a.first < b.first; } vector<vector<long long> > g(arr); int main(void) { long long n; cin >> n; for (int i = 0; i < n - 1; i++) { long long l, r; cin >> l >> r; g[l].push_back(r); g[r].push_back(l); } set<long long> st; for (long long i = 2; i <= n; i++) { if (g[i].size() == 2) return cout << "NO" << '\n', 0; st.insert(g[i].size()); } if (st.size() != 1) return cout << "NO" << '\n', 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #include <vector> #include <set> #include <map> #include <string> #include <cstdio> #include <cstdlib> #include <climits> #include <utility> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <iomanip> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> //setbase - cout << setbase (16); cout << 100 << endl; Prints 64 //setfill - cout << setfill ('x') << setw (5); cout << 77 << endl; prints xxx77 //setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx //cout.precision(x) cout<<fixed<<val; // prints x digits after decimal in val using namespace std; using namespace __gnu_pbds; // find_by_order() // order_of_key typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define f(i,a,b) for(ll i=a;i<b;i++) #define fi(i,a,b) for(int i=a;i<b;i++) #define rep(i,n) f(i,0,n) #define fd(i,a,b) for(ll i=a;i>=b;i--) #define fc(i,a,b) for(ll i=a;i<=b;i++) #define fp(i,a,n) for(ll i=n-1; i>a ;i--) #define pb push_back #define mp make_pair #define vi vector< int > #define vl vector< ll > #define ss second #define ff first #define ll long long #define pii pair< int,int > #define pll pair< ll,ll > #define sz(a) a.size() #define inff (1000*1000*1000+5) #define all(a) a.begin(),a.end() #define tri pair<int,pii> #define vii vector<pii> #define vll vector<pll> #define viii vector<tri> #define pqueue priority_queue< int > #define pdqueue priority_queue< int,vi ,greater< int > > #define flush fflush(stdout) #define primeDEN 727999983 #define SPEED ios_base::sync_with_stdio(false) ;cin.tie(NULL) ;cout.tie(NULL) ; #define deb(x) cout << #x << " ---> " << x << endl; #define X first #define Y second #define pia 3.1415926536 #define mod 1000000007 #define ml map<ll,ll> #define mi map<int,int> #define mll map<pll,ll> #define vvl vector<vl> #define vvi vector<vi> #define mi map<int,int> #define letsh(a,b) a<<b #define rigsh(a,b) a>>b mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count()) ; ll lcm(ll a, ll b) { return a / __gcd(a, b) * b; } bool sortbysec(const ll &a, const ll &b) { return (a> b); } ll power(ll a,ll n) { ll ans=1; while(n) { if(n&1) ans=(ans*a)%mod; a=(a*a)%mod ; n=n>>1; } return ans; } ll power2(ll a,ll n) { ll ans =1 ; while(n) { if(n&1) ans =(ans*a) ; a = (a*a) ; n =n>>1 ; } return ans ; } #define N 1e5 +50 vvl g(N) ; int main() { SPEED ll n; cin>>n ; f(i,0,n-1) { ll u,v ;cin>>u>>v ; g[u].pb(v) ;g[v].pb(u) ; } ll f=1 ; f(i,1,n+1) { if(g[i].size() == 2) { for(auto e:g[i]) { if(g[e].size() ==1) f=0 ; } if(!f) break ; } } if(f) 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" ] }
IN-CORRECT
python3
n = int(input()) edges = [0]*(n+1) isPossible = True for i in range(n-1): a,b = [int(i) for i in input().split()] edges[a]+=1 edges[b]+=1 for k in edges: if edges[k] == 2: isPossible = False break print("YES" if isPossible 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int x, y, n; cin >> n; vector<int> v[100010], barg; for (int i = 0; i < n - 1; i++) { cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } for (int i = 1; i <= n; i++) { if (v[i].size() % 2 == 0) { 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" ] }
IN-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); } m = 0; for (i = 0; i < n; i++) { if (G[i].size() == 1) m++; } if (((m * (m - 1)) / 2) >= n - 1) 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" ] }
IN-CORRECT
java
import java.util.*; import java.io.*; public class D572 { public static void main(String[] args) { MyScanner sc = new MyScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int n = sc.nextInt(); 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++) { int x = sc.nextInt(); int y = sc.nextInt(); adj[x].add(y); adj[y].add(x); } if (n == 2) { out.println("YES"); } else { Set<Integer> par = new HashSet<>(); for (int i = 1; i <= n; i++) { if (adj[i].size() == 1) par.add(adj[i].get(0)); } boolean ok = true; for (Integer i: par) { if (adj[i].size() == 2) ok = false; } out.println(ok ? "YES" :"NO"); } out.close(); } //-----------MyScanner class for faster input---------- 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" ] }
IN-CORRECT
python3
from sys import stdin input = stdin.readline n = int(input()) degree = [0 for i in range(n+1)] for _ in range(n-1): i, j = [int(i) for i in input().split()] degree[i] += 1 degree[j] += 1 res = False for i in range(1, n+1): if degree[i] == 2: res = True if res: 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" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.StringTokenizer; public class D { static FastReader scan; static PrintWriter out; public static void main(String[] args) throws FileNotFoundException { Solver solver = new Solver(); scan = new FastReader(); out = new PrintWriter(System.out); int testCases = 1; for(int i = 1; i <= testCases; i++) { // out.print("Case #" + i + ": "); solver.solve(); } out.close(); } static class Solver { static ArrayList<Integer>[] adj; static int[] level; static int[] count; void solve() { int n = scan.nextInt(); adj = new ArrayList[n]; level = new int[n]; for(int i = 0; i < n; i++) adj[i] = new ArrayList<>(); for(int i = 0; i < n-1; i++) { int u = scan.nextInt()-1, v = scan.nextInt()-1; adj[u].add(v); adj[v].add(u); } boolean[] v = new boolean[n]; go(0,0,v); int c = 0; for(int i = 0; i < n; i++) if(adj[i].size() !=0) c+= level[i]; out.println(c%2==1?"YES":"NO"); } static void go(int curr, int l, boolean[] v) { v[curr] = true; if(adj[curr].size() == 0) { level[curr] = 1; } for(int i : adj[curr]) { if(!v[i]) { go(i,l+1,v); level[curr] += level[i]; } } } } // Sathvik's Template Stuff BELOW!!!!!!!!!!!!!!!!!!!!!! static class DSU { int[] root, size; int n; DSU(int n) { this.n = n; root = new int[n]; size = new int[n]; for (int i = 0; i < n; i++) { root[i] = i; size[i] = 1; } } int findParent(int idx) { while (root[idx] != idx) { root[idx] = root[root[idx]]; idx = root[idx]; } return idx; } boolean union(int x, int y) { int parX = findParent(x); int parY = findParent(y); if (parX == parY) return false; if (size[parX] < size[parY]) { root[parY] = parX; size[parX] += size[parY]; } else { root[parX] = parY; size[parY] += size[parX]; } return true; } } static class Extra { static void sort(int[] a) { Integer[] aa = new Integer[a.length]; for (int i = 0; i < aa.length; i++) aa[i] = a[i]; Arrays.sort(aa); for (int i = 0; i < aa.length; i++) a[i] = aa[i]; } static void sort(long[] a) { Long[] aa = new Long[a.length]; for (int i = 0; i < aa.length; i++) aa[i] = a[i]; Arrays.sort(aa); for (int i = 0; i < aa.length; i++) a[i] = aa[i]; } static void sort(double[] a) { Double[] aa = new Double[a.length]; for (int i = 0; i < aa.length; i++) aa[i] = a[i]; Arrays.sort(aa); for (int i = 0; i < aa.length; i++) a[i] = aa[i]; } static void sort(char[] a) { Character[] aa = new Character[a.length]; for (int i = 0; i < aa.length; i++) aa[i] = a[i]; Arrays.sort(aa); for (int i = 0; i < aa.length; i++) a[i] = aa[i]; } static long gcd(long a, long b) { while (b > 0) { long temp = b; b = a % b; a = temp; } return a; } static long lcm(long a, long b) { return a * (b / gcd(a, b)); } static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long i = 5; i * i <= n; i = i + 6) { if (n % i == 0 || n % (i + 2) == 0) return false; } return true; } static HashSet<Integer> sieve(int n) { boolean[] prime = new boolean[n + 1]; HashSet<Integer> res = new HashSet<>(); for (int p = 2; p * p <= n; p++) { if (!prime[p]) { res.add(p); for (int i = p * p; i <= n; i += p) prime[i] = true; } } return res; } static HashMap<Long, Integer> primeFactorization(long n) { HashMap<Long, Integer> res = new HashMap<>(); while (n % 2 == 0) { res.put(2L, res.getOrDefault(2L, 0) + 1); n /= 2; } for (long i = 3; i * i <= n; i += 2) { while (n % i == 0) { res.put(i, res.getOrDefault(i, 0) + 1); n /= i; } } if (n > 2) res.put(n, 1); return res; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 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()); } int[] nextIntArray(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()); } long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } double nextDouble() { return Double.parseDouble(next()); } double[] nextDoubleArray(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextDouble(); return a; } 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" ] }
IN-CORRECT
cpp
#include <iostream> #include <string> #include <algorithm> #include <math.h> #include <vector> #include <string> #include <string.h> #include <set> #include <queue> #include <map> #include <stack> #include <functional> #include <deque> #include <conio.h> using namespace std; const int INF = (int)1e8; int main() { int n; cin >> n; vector<vector<int>> g(n); for (int i = 0; i < n-1; i++) { int a, b; cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); } for (int i = 0; i < n; i++) { int size = g[i].size(); if (size % 2 == 0) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; //_getch(); 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" ] }
IN-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 ((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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse4") const long double EPS = 1e-9; const long long INF = 1e9; const long long mod = 1e9 + 7; using namespace std; mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); long long n, m, k; long long q; vector<vector<long long>> vec; vector<bool> used; vector<long long> cnt; vector<long long> ans; vector<long long> pref; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n; vec.resize(n); cnt.resize(n, 0); for (int i = 0; i < n - 1; ++i) { int v, to; cin >> v >> to; --v; --to; vec[v].push_back(to); vec[to].push_back(v); cnt[v]++; cnt[to]++; } int cntl = 0, cntnl = 0; for (int i = 0; i < n; ++i) { if (cnt[i] == 1) cntl++; else { int cntal = 0; for (int j = 0; j < vec[i].size(); ++j) { if (cnt[vec[i][j]] == 1) { cntal++; } } if (cntal <= 1) { cntnl++; } } } if ((cntl == 2 && n > 2) || cntnl || (cntl == n - 1 && cntl % 2)) { cout << "NO"; } else { 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int main() { long long in[100005]; long long n, i, j, m, t; memset(in, 0, sizeof(in)); scanf("%lld", &n); for (i = 1; i <= n - 1; i++) { int u, v; scanf("%lld %lld", &u, &v); in[u]++; in[v]++; } long long num = 0; long long ans = 0; for (i = 1; i <= n; i++) if (in[i] == 1) num++; num--; ans = (1 + num) * num / 2; if (ans >= n - 1) 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long int inf = 1e18, M = 1e9 + 7; const long long int N = 1e5 + 5; long long int tot; vector<long long int> v[N], up(N, 0), dn(N, 0); bool dfs(long long int x, long long int p) { long long int cnt = 0; for (auto u : v[x]) { if (u == p) ; else { if (!dfs(u, x)) return 0; } if (v[u].size() == 1ll) ++cnt; } if (cnt == tot || (cnt == 1 && v[p].size() > 1)) return 0; return 1; } void solve() { long long int n; cin >> n; long long int a, b; for (long long int i = 1; i < n; ++i) { cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } tot = 0ll; for (long long int i = 1; i <= n; ++i) { if (v[i].size() == 1ll) ++tot; } if (!dfs(1, -1)) cout << "NO"; else cout << "YES"; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long int t = 1; 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" ] }
IN-CORRECT
python3
n=int(input()) tree=[[] for i in range(n)] for i in range(n-1): u,v=map(int,input().split()) tree[u-1].append(v-1) tree[v-1].append(u-1) if n==2: print("YES") exit() #nが4δ»₯上 check=[0]*n for i in range(n): if len(tree[i])==1: check[tree[i][0]]+=1 if len(tree[i])==2: print("NO") exit() for i in range(n): if check[i]==1: 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n; vector<int> g[N]; bool ar[N]; void dfs(int u, int prev) { int cnt = 0; for (auto v : g[u]) { if (v == prev) continue; if (g[v].size() == 1) cnt++; dfs(v, u); } if (g[u].size() == 1) ar[u] = 1; else if (cnt > 1) ar[u] = 1; } int main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); cin >> n; for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } dfs(1, 0); int bo = 1; for (int i = 1; i <= n; i++) { if (!ar[i]) bo = 0; } if (bo) 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" ] }
IN-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) { mp[x.size() % 2] += 1; } long long ans = mp[1]; if (n % 2 == 0 && ans % 2 == 0) ok(); else 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int MAX = 262144; const int END = 200000; const int INF = 1e9 + 7; int n; int arr[100011]; int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int x, y; scanf("%d %d", &x, &y); arr[x]++; arr[y]++; } int cnt = 0; for (int i = 1; i <= n; i++) { if (arr[i] == 1) cnt++; } int cnt2 = 0; for (int i = 1; i <= n; i++) { if (arr[i] == 2) cnt2++; } if (cnt * (cnt - 1) / 2 < (n - 1)) printf("NO\n"); else { if (cnt2 > 0) printf("NO\n"); else 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; int n, x, y, sons[100010]; vector<int> g[100010]; int dfs(int pos, int no) { for (int i = 0; i < g[pos].size(); i++) if (g[pos][i] != no) sons[pos] += dfs(g[pos][i], pos); return sons[pos] + (g[pos].size() == 1 ? 1 : 0); } void dfs2(int pos, int no) { for (int i = 0; i < g[pos].size(); i++) { if (g[pos][i] == no) continue; if (sons[g[pos][i]] == 1 || n - sons[g[pos][i]] - (g[pos].size() == 1 ? 1 : 0) - (g[g[pos][i]].size() == 1 ? 1 : 0) == 1) { puts("NO"); exit(0); } dfs(g[pos][i], pos); } } 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); } dfs(1, 0); dfs2(1, 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" ] }
IN-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())==1)c++; } if(n==2)w.print("YES"); else if(n==3 && c==2)w.print("NO"); else if(n==3 && c==3)w.print("YES"); else if(c%2==0)w.print("YES"); else w.print("NO"); 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" ] }
IN-CORRECT
java
import java.util.*; import java.lang.*; import java.io.*; public class Main { 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; } } static int MOD=1000000000+7; //Brian Kernighan’s Algorithm static long countSetBits(long n){ if(n==0) return 0; return 1+countSetBits(n&(n-1)); } //Factorial static long factorial(long n){ if(n==1) return 1; if(n==2) return 2; if(n==3) return 6; return n*factorial(n-1); } //Euclidean Algorithm static long gcd(long A,long B){ if(B==0) return A; return gcd(B,A%B); } //Modular Exponentiation static long fastExpo(long x,long n){ if(n==0) return 1; if((n&1)==0) return fastExpo((x*x)%MOD,n/2); return ((x%MOD)*fastExpo((x*x)%MOD,(n-1)/2)); } //AKS Algorithm static boolean isPrime(long n){ if(n<=1) return false; if(n<=3) return true; if(n%2==0 || n%3==0) return false; for(int i=5;i*i<=n;i+=6) if(n%i==0 || n%(i+2)==0) return false; return true; } //Sieve of eratosthenes static int[] findPrimes(int n){ boolean isPrime[]=new boolean[n+1]; ArrayList<Integer> a=new ArrayList<>(); int result[]; Arrays.fill(isPrime,true); isPrime[0]=false; isPrime[1]=false; for(int i=2;i*i<=n;++i){ if(isPrime[i]==true){ for(int j=i*i;j<=n;j+=i) isPrime[j]=false; } } for(int i=0;i<=n;i++) if(isPrime[i]==true) a.add(i); result=new int[a.size()]; for(int i=0;i<a.size();i++) result[i]=a.get(i); return result; } static int V; static ArrayList<Integer> graph[]; static void initialize(int n){ V=n; graph=new ArrayList[V]; for(int i=0;i<V;i++) graph[i]=new ArrayList<>(); } static void addEdge(int src,int dest){ graph[src].add(dest); graph[dest].add(src); } static boolean solve(int s,int p){ if(p!=-1 && graph[p].size()<=2) return false; for(Integer child: graph[s]){ if(child==p) continue; return solve(child,s); } return true; } public static void main (String[] args) throws java.lang.Exception { FastReader sc=new FastReader(); int n=sc.nextInt(); if(n==2){ System.out.println("YES"); System.exit(0); } initialize(n); int root=sc.nextInt()-1; addEdge(root,sc.nextInt()-1); for(int i=1;i<n-1;i++) addEdge(sc.nextInt()-1,sc.nextInt()-1); String ans=solve(root,-1)==true?"YES":"NO"; System.out.println(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" ] }
IN-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<>(); for(int i=1;i<n;i++){ int u=ni(),v=ni(); g[u].add(v); g[v].add(u); } if(n==2){ pw.println("YES"); return; } dfs(1,-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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; vector<int> v[101010]; int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int a, b; scanf("%d %d", &a, &b); v[a].push_back(b); v[b].push_back(a); } for (int i = 1; i <= n; i++) if (!(v[i].size() % 2)) return !printf("NO"); 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int N; vector<long long> adj[100001]; bool yes = 1; int rt; void dfs(int x, int p) { if (p != -1) { int num = adj[p].size() - 1; if (p != rt) num--; if (num <= 0) yes = 0; } for (auto i : adj[x]) { if (i != p) dfs(i, x); } } int main() { ios_base::sync_with_stdio(false), cin.tie(0); cin >> N; for (long long i = 1; i <= long long(N - 1); ++i) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } for (long long i = 1; i <= long long(N); ++i) { if (adj[i].size() > 1) rt = i; } if (rt == 0) { cout << "YES"; return 0; } cout << rt << '\n'; dfs(rt, -1); if (yes) cout << "YES"; else cout << "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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) {} template <typename S37, typename... Args> void err(istream_iterator<string> it, S37 a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } const long long N = 200010, mod = 1e9 + 7, mod2 = 1e9 + 9, mod3 = 998244353, sq = 450, base = 37, lg = 25, inf = 1e18 + 10, del = 67733; long long n, m, x, y, w, z, X, Y, Z, t, k, ans, a[N]; vector<long long> v[N]; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 0; i < n - 1; i++) { cin >> x >> y; v[x].push_back(y); a[x]++; a[y]++; v[y].push_back(x); } if (n == 2) return cout << "YES", 0; y = 0; for (int i = 1; i <= n; i++) { x = 0; if (a[i] == 1) continue; y++; for (int j = 0; j < v[i].size(); j++) if (a[v[i][j]] == 1) x++; if (x % 2 == 1 || x == 0) return cout << "NO", 0; } if (y == 1) return cout << "NO", 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" ] }
IN-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); } if(n==2) out.println("YES"); boolean[] jud=new boolean[n+1]; for(int i=1;i<=n;i++) { if(map[i].size()==0) 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" ] }
IN-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 nb_leafs = 0 nb_other = 0 for x in edges: if x == 1: nb_leafs += 1 else: nb_other += 1 if (nb_leafs >= nb_other + 2): 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" ] }
IN-CORRECT
python3
from sys import stdin input=stdin.readline n=int(input()) a=[[] for i in range(n)] for i in range(n-1): c,d=map(int,input().split()) a[c-1].append(d-1) a[d-1].append(c-1) if n==2: print('YES') else: k=0 for i in a: if len(i)%2==0: exit(print('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" ] }
IN-CORRECT
cpp
#include<bits/stdc++.h> using namespace std; #define int long long vector<int> graph[(int)(1e5+5)]; vector<int> level(1e5); bool marked[(int)(1e5+5)]; void bfs(int x) { queue<int> que; que.push(x); level[x] = 0; marked[x] = true; while (!que.empty()) { x = que.front(); que.pop(); for (int i = 0; i < graph[x].size(); i++) { int b = graph[x][i]; if (!marked[b]) { que.push(b); level[b] = level[x] + 1; marked[b] = true; } } } } int32_t main() { int n; cin>>n; int u,v; int deg[n+1]={0}; for(int i=1;i<n;i++) { cin>>u>>v; deg[u]++; deg[v]++; graph[u].push_back(v); graph[v].push_back(u); } if(n==2) { cout<<"YES\n"; return 0; } if(n==3) { cout<<"NO\n"; return 0; } bfs(1); int leaf[(int)(1e5+5)]={0}; for(int i=1;i<=n;i++) { if(deg[i]==1) { leaf[level[i]]++; } } for(int i=0;i<(int)(1e5+5);i++) { if(leaf[i]<2 && leaf[i]!=0) { cout<<"NO\n"; return 0; } } cout<<"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" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class d2prac implements Runnable { private boolean console=false; boolean ans=true; int sc=0; ArrayList<Integer> adj[]; public void solve() { int i; int n=in.ni(); adj=new ArrayList [n]; for(i=0;i<n;i++) adj[i]=new ArrayList(); for(i=0;i<n-1;i++) { int u=in.ni()-1; int v=in.ni()-1; adj[u].add(v); adj[v].add(u); } if(n==2) { out.println("YES"); return; } dfs(0,-1); if(ans&&sc>1) out.println("YES"); else out.println("NO"); } public void dfs(int v,int p) { boolean not=false; if(adj[v].size()>1) not=true; int count=0; for(int node:adj[v]) { if(adj[node].size()==1) count++; if(node==p) continue; else dfs(node,v); } if(not&&count<2) ans=false; else if(not) sc++; } @Override public void run() { try { init(); } catch (FileNotFoundException e) { e.printStackTrace(); } int t= 1; while (t-->0) { solve(); out.flush(); } } private FastInput in; private PrintWriter out; public static void main(String[] args) throws Exception { new d2prac().run(); } private void init() throws FileNotFoundException { InputStream inputStream = System.in; OutputStream outputStream = System.out; try { if (!console && System.getProperty("user.name").equals("sachan")) { outputStream = new FileOutputStream("/home/sachan/Desktop/output.txt"); inputStream = new FileInputStream("/home/sachan/Desktop/input.txt"); } } catch (Exception ignored) { } out = new PrintWriter(outputStream); in = new FastInput(inputStream); } static class FastInput { InputStream obj; public FastInput(InputStream obj) { this.obj = obj; } byte inbuffer[] = new byte[1024]; int lenbuffer = 0, ptrbuffer = 0; int readByte() { if (lenbuffer == -1) throw new InputMismatchException(); if (ptrbuffer >= lenbuffer) { ptrbuffer = 0; try { lenbuffer = obj.read(inbuffer); } catch (IOException e) { throw new InputMismatchException(); } } if (lenbuffer <= 0) return -1;return inbuffer[ptrbuffer++]; } String ns() { int b = skip();StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { sb.appendCodePoint(b);b = readByte(); }return sb.toString();} 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(); }} 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 * 10L + (b - '0'); } else { return minus ? -num : num; }b = readByte(); } } boolean isSpaceChar(int c) { return (!(c >= 33 && c <= 126)); } int skip() { int b;while ((b = readByte()) != -1 && isSpaceChar(b)) ;return b; } float nf() {return Float.parseFloat(ns());} double nd() {return Double.parseDouble(ns());} char nc() {return (char) skip();} } }
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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void dfs(vector<int> AdjL[], int visited[], int source, long long int &ans) { if (visited[source] == 1) return; visited[source] = 1; if (AdjL[source].size() == 1) ans++; for (int i = 0; i < AdjL[source].size(); ++i) { if (!visited[AdjL[source][i]]) { dfs(AdjL, visited, AdjL[source][i], ans); } } } int main() { long long int n; cin >> n; vector<int> AdjL[n + 1]; for (int i = 0; i < n - 1; ++i) { int x, y; cin >> x >> y; AdjL[x].push_back(y); AdjL[y].push_back(x); } long long int ans = 0, path; int visited[n + 1]; fill_n(visited, n + 1, 0); dfs(AdjL, visited, 1, ans); path = (ans * (ans - 1)) / 2; if (path >= n - 1) cout << "YES"; else cout << "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" ] }
IN-CORRECT
python3
N = int(input()) adj_list = [[] for _ in range(N)] for _ in range(N-1): (u,v) = list(map(int, input().split())) u -= 1 v -= 1 adj_list[u].append(v) adj_list[v].append(u) num_leaf = 0 for node in range(N): if len(adj_list[node]) ==1: num_leaf += 1 if N-1 <= (num_leaf*(num_leaf-1))//2: 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" ] }
IN-CORRECT
python3
# -*- coding: utf-8 -*- import sys # from operator import itemgetter # from fractions import gcd # from math import ceil, floor # from copy import deepcopy # from itertools import accumulate from collections import deque # import math # from functools import reduce input = sys.stdin.readline def ii(): return int(input()) def mi(): return map(int, input().rstrip().split()) def lmi(): return list(map(int, input().rstrip().split())) def li(): return list(input().rstrip()) # template # BEGIN CUT HERE class Graph(): def __init__(self, n, Weighted=False, Directed=True, Matrix=False): self.sz = n self.is_Weighted = Weighted self.is_Directed = Directed self.is_Matrix = Matrix if Matrix: if Weighted: self.graph = [[0 for _i in range(n)] for _j in range(n)] else: self.graph = [[0 for _i in range(n)] for _j in range(n)] else: self.graph = [[] for _i in range(n)] def _weighted_add_edge(self, x, y, w): if self.is_Matrix: self.graph[x][y] = w else: self.graph[x].append((y, w)) def _unweighted_add_edge(self, x, y): if self.is_Matrix: self.graph[x][y] = 1 else: self.graph[x].append(y) def add_edge(self, x, y, *w): if self.is_Directed: if self.is_Weighted: self._weighted_add_edge(x, y, w[0]) else: self._unweighted_add_edge(x, y) else: if self.is_Weighted: self._weighted_add_edge(x, y, w[0]) self._weighted_add_edge(y, x, w[0]) else: self._unweighted_add_edge(x, y) self._unweighted_add_edge(y, x) def _convert_to_maxrix(self): if self.is_Matrix: return self mat_g = self.__class__( self.sz, Weighted=self.is_Weighted, Directed=self.is_Directed, Matrix=True) if self.is_Weighted: for i in range(self.sz): for j in self.graph[i]: mat_g.add_edge(i, j[0], j[1]) else: for i in range(self.sz): for j in self.graph[i]: mat_g.add_edge(i, j) return mat_g def __getitem__(self, n): return self.graph[n] # def __setitem__(self, n, v): # if not self.is_Weighted and not self.is_Matrix: # self.graph[n] = v def __str__(self): return str([self.graph[i] for i in range(self.sz)]) def main(): n = ii() g = Graph(n, Directed=False) for i in range(n - 1): s, t = mi() g.add_edge(s-1, t-1) for i in range(n): if len(g[i]) % 2 == 0: break else: print('YES') sys.exit() print('NO') if __name__ == '__main__': 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" ] }
IN-CORRECT
java
import java.util.*; import java.io.*; public class d { public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(System.in); int n = sc.nextInt(); ArrayList<Integer>[] paths = new ArrayList[n]; for (int i = 0 ; i < n ; i++) { paths[i] = new ArrayList<>(); } for (int i = 0 ; i < n-1 ; i++) { int a = sc.nextInt()-1, b = sc.nextInt()-1; paths[a].add(b); paths[b].add(a); } for (int i = 0 ; i < paths.length ; i++) { if (paths[i].size() == 1) { System.out.println("NO"); return; } } System.out.println("YES"); } /* 3 1 2 1 3 */ static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream i) { br = new BufferedReader(new InputStreamReader(i)); st = new StringTokenizer(""); } public String next() throws IOException { if(st.hasMoreTokens()) return st.nextToken(); else st = new StringTokenizer(br.readLine()); return next(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(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" ] }
IN-CORRECT
python3
from collections import defaultdict, deque, Counter from sys import stdin, stdout from heapq import heappush, heappop import math import io import os import math import bisect #?############################################################ def isPrime(x): for i in range(2, x): if i*i > x: break if (x % i == 0): return False return True #?############################################################ def ncr(n, r, p): num = den = 1 for i in range(r): num = (num * (n - i)) % p den = (den * (i + 1)) % p return (num * pow(den, p - 2, p)) % p #?############################################################ def primeFactors(n): l = [] while n % 2 == 0: l.append(2) n = n / 2 for i in range(3, int(math.sqrt(n))+1, 2): while n % i == 0: l.append(int(i)) n = n / i if n > 2: l.append(n) return list(set(l)) #?############################################################ def power(x, y, p): res = 1 x = x % p if (x == 0): return 0 while (y > 0): if ((y & 1) == 1): res = (res * x) % p y = y >> 1 x = (x * x) % p return res #?############################################################ def sieve(n): prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 return prime #?############################################################ def digits(n): c = 0 while (n > 0): n //= 10 c += 1 return c #?############################################################ def ceil(n, x): if (n % x == 0): return n//x return n//x+1 #?############################################################ def mapin(): return map(int, input().split()) #?############################################################ # input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline # python3 15.py<in>op n = int(input()) d = [0]*n for _ in range(n-1): u, v = mapin() d[u-1]+=1 # d[v-1]-=1 if(d.count(2) == 0): 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" ] }
IN-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() == 1) { int j = adj[i][0]; if (adj[j].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" ] }
IN-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() == 1) { if (a[a[i][0]].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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; int n, x, y, sons[100010], all; vector<int> g[100010]; int dfs(int pos, int no) { for (int i = 0; i < g[pos].size(); i++) if (g[pos][i] != no) sons[pos] += dfs(g[pos][i], pos); return sons[pos] + (g[pos].size() == 1 ? 1 : 0); } void dfs2(int pos, int no) { for (int i = 0; i < g[pos].size(); i++) { if (g[pos][i] == no) continue; if (sons[g[pos][i]] == 1 || all - sons[g[pos][i]] - (g[pos].size() == 1 ? 1 : 0) - (g[g[pos][i]].size() == 1 ? 1 : 0) == 1) { puts("NO"); exit(0); } dfs(g[pos][i], pos); } } 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); } dfs(1, 0); all = sons[1] + (g[1].size() == 1 ? 1 : 0); dfs2(1, 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long inf = LONG_MAX; const long long arr = 1000000; bool comp(pair<long long, long long> a, pair<long long, long long> b) { if (a.first == b.first) return a.second < b.second; else return a.first < b.first; } vector<vector<long long> > g(arr); int main(void) { long long n; cin >> n; for (int i = 0; i < n - 1; i++) { long long l, r; cin >> l >> r; g[l].push_back(r); g[r].push_back(l); } if (n % 2 == 1) return cout << "NO" << '\n', 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long max_n = 1e6 + 20; long long n, m, k, ans, sum; long long a[max_n]; long long mark[max_n]; vector<long long> v, adj[max_n], jda[max_n]; void dfs(long long v) { mark[v] = 1; if (jda[v].size() == 1 && adj[v].size() == 1) ans = -1; if (jda[v].size() == 2 && adj[v].size() == 0) ans = -1; for (auto i : adj[v]) { if (!mark[i]) { dfs(i); } } } int32_t main() { cin >> n; for (long long i = 1; i < n; i++) { long long u, v; cin >> u >> v; u--, v--; adj[v].push_back(u); jda[u].push_back(v); } if (n == 2) { cout << "YES"; return 0; } for (long long i = 0; i < n; i++) { if (jda[i].size() == 0) { dfs(i); } } if (ans == -1) { cout << "NO"; } else 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long inf = LONG_MAX; const long long arr = 1000000; bool comp(pair<long long, long long> a, pair<long long, long long> b) { if (a.first == b.first) return a.second < b.second; else return a.first < b.first; } vector<vector<long long> > g(arr); int main(void) { long long n; cin >> n; for (int i = 0; i < n - 1; i++) { long long l, r; cin >> l >> r; g[l].push_back(r); g[r].push_back(l); } for (long long i = 2; i <= n; i++) { if (g[i].size() == 2 || (g[i].size() == 1 && n != 2)) return cout << "NO" << '\n', 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" ] }
IN-CORRECT
java
import java.io.*; import java.math.BigInteger; import java.util.*; import java.util.Stack; import java.util.regex.Pattern; public class ROUGH { public static class FastReader { BufferedReader br; StringTokenizer st; //it reads the data about the specified point and divide the data about it ,it is quite fast //than using direct public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception r) { r.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next());//converts string to integer } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (Exception r) { r.printStackTrace(); } return str; } } public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out)); static long mod = (long)(1e9+7); static long N = (long) 1e18; public static void main(String[] args) { FastReader sc = new FastReader(); int n = sc.nextInt(); List<Integer>[] edge = new ArrayList[n+1]; for(int i=0;i<edge.length;++i) edge[i] = new ArrayList<>(); for(int i=1;i<n;++i) { int u = sc.nextInt(); int v = sc.nextInt(); edge[u].add(v); edge[v].add(u); } boolean found = true; for(int i=1;i<=n && found;++i) { if(edge[i].size() == 1 || edge[i].size() == 3) continue; else found = false; } if(found) out.println("YES"); else out.println("NO"); 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) {} template <typename S37, typename... Args> void err(istream_iterator<string> it, S37 a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } const long long N = 200010, mod = 1e9 + 7, mod2 = 1e9 + 9, mod3 = 998244353, sq = 450, base = 37, lg = 25, inf = 1e18 + 10, del = 67733; long long n, m, x, y, w, z, X, Y, Z, t, k, ans, a[N]; vector<long long> v[N]; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 0; i < n - 1; i++) { cin >> x >> y; v[x].push_back(y); a[x]++; a[y]++; v[y].push_back(x); } if (n == 2) return cout << "YES", 0; if (n == 3) return cout << "NO", 0; for (int i = 1; i <= n; i++) { x = 0; if (a[i] == 1) continue; for (int j = 0; j < v[i].size(); j++) if (a[v[i][j]] == 1) x++; if (x % 2 || x == 0) return cout << "NO", 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" ] }
IN-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] == 1) { for (auto j : a[i]) { if (d[j] == 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" ] }
IN-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) { mp[x.size() % 2] += 1; } long long ans = mp[1]; if (n % 2 == ans % 2) ok(); else 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void fast(string name = "") { ios_base::sync_with_stdio(0); cin.tie(0); if ((int)name.size() > 0) { freopen((name + ".in").c_str(), "r", stdin); freopen((name + ".out").c_str(), "w", stdout); } } const int N = 1e5; int deg[N]; int n; void solve() { cin >> n; int a, b; fill(deg, deg + n + 1, 0); for (int i = 1; i < n; ++i) { cin >> a >> b; deg[a]++; deg[b]++; } int leaf = 0; for (int i = 1; i <= n; ++i) { if (deg[i] == 1) { leaf++; } } if ((leaf * (leaf - 1)) >= 2 * n - 2) { cout << "YES\n"; } else { cout << "NO\n"; } } int main() { fast(""); 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" ] }
IN-CORRECT
java
import java.util.*; public class Graph{ int V; ArrayList<Integer>[] adj; Graph(int v){ V=v; adj=new ArrayList[V+1]; for(int i=1;i<V+1;i++) { adj[i]=new ArrayList<>(); } } void addEdge(int a,int b) { adj[a].add(b); adj[b].add(a); } void check() { int ans=1; int leaf=0; for(int i=1;i<adj.length;i++) { if(adj[i].size()>=3) { ans=0; } if(adj[i].size()==1) { leaf++; } } if(ans==0 && leaf%2==0) { System.out.println("YES"); }else if(ans==0 && leaf%2!=0){ System.out.println("NO"); }else { System.out.println("NO"); } } public static void main(String[] args) { Scanner ip=new Scanner(System.in); int n=ip.nextInt(); Graph g=new Graph(n); for(int i=1;i<n;i++) { int p=ip.nextInt(); int c=ip.nextInt(); g.addEdge(p,c); } if(n==2) { System.out.println("YES"); }else { g.check(); } } }
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" ] }
IN-CORRECT
java
/* Roses are red Memes are neat All my test cases time out Lmao yeet */ import java.util.*; import java.io.*; public class D { public static void main(String args[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); LinkedList<Integer>[] edges = new LinkedList[N+1]; for(int i=1; i <= N; i++) edges[i] = new LinkedList<Integer>(); for(int i=0; i < N-1; i++) { st = new StringTokenizer(infile.readLine()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); edges[a].add(b); edges[b].add(a); } boolean b = true; for(int i=1; i <= N; i++) if(edges[i].size()%2 == 0) b = false; if(b) System.out.println("YES"); else System.out.println("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" ] }
IN-CORRECT
python3
# https://codeforces.com/contest/1189/problem/D1 n = int(input()) g = {} p = {} path = {} flg = True for _ in range(n-1): u,v = map(int, input().split()) if u not in g: g[u] = [] g[u].append(v) if v not in g: g[v] = [] g[v].append(u) def solve(g, p, path): if len(g) == 2: return True if len(g) == 3: return False root = None for u in g: if len(g[u]) >= 2: root = u break S = [root] p[root] = 0 i = 0 while i < len(S): cur = S[i] for x in g[cur]: if x == p[cur]:continue p[x] = cur S.append(x) i+=1 for cur in S[::-1]: if len(g[cur]) == 1: path[cur] = 0 else: cnt = 0 for x in g[cur]: if x == p[cur]:continue if path[x] == 1: return False cnt += 1 path[cur] = cnt return True flg = solve(g, p, path) if flg == True: 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long int inf = 1e18, M = 1e9 + 7; const long long int N = 1e5 + 5; long long int tot; vector<long long int> v[N]; bool dfs(long long int x, long long int p) { long long int cnt = 0; for (auto u : v[x]) { if (u == p) ; else { if (!dfs(u, x)) return 0; } if (v[u].size() == 1ll) ++cnt; } if ((cnt == tot && tot == 2ll) || (cnt == 1 && v[p].size() > 1)) return 0; return 1; } void solve() { long long int n; cin >> n; long long int a, b; for (long long int i = 1; i < n; ++i) { cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } tot = 0ll; for (long long int i = 1; i <= n; ++i) { if (v[i].size() == 1ll) ++tot; } if (!dfs(1, -1)) cout << "NO"; else cout << "YES"; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long int t = 1; 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> long long mod = 998244353; using namespace std; const long long N = 1e5 + 5; vector<long long> g[N]; long long n; void fail() { cout << "NO\n"; exit(0); } long long cnt; void dfs(long long node, long long p = 0) { if (g[node].size() == 1) cnt++; for (long long nx : g[node]) { if (nx == p) continue; dfs(nx, node); } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; if (n == 2) { cout << "YES\n"; return 0; } if (n == 3) fail(); for (long long i = 1, u, v; i < n; i++) { cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } dfs(1); if (cnt % 2 == 0) cout << "YES"; else cout << "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" ] }
IN-CORRECT
cpp
#pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define ll long long int #define pb push_back #define mp make_pair #define ld long double #define sz(a) (ll)(a).size() #define endl "\n" typedef tree<pair<ll,ll> ,null_type,less<pair<ll,ll> >,rb_tree_tag,tree_order_statistics_node_update> ordered_set; //K-th smallest //cout << k << "kth smallest: " << *A.find_by_order(k-1) << endl; //NO OF ELEMENTS < X //cout << "No of elements less than " << X << " are " << A.order_of_key(X) << endl; const int MAXN = 100005; vector<ll>adj[MAXN]; bool f = true; void dfs(ll node, ll par) { ll count = 0; for(auto it : adj[node]) { if(it != par) { dfs(it, node); count++; } } if(count == 1 && par != -1) f = false; } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n; cin >> n; for(int i=0;i<n-1;i++) { ll u, v; cin >> u >> v; adj[u - 1].pb(v - 1); adj[v - 1].pb(u - 1); } if(n == 2) cout << "YES" << endl; else if(n == 3) cout << "NO" << endl; else { dfs(0, -1); if(f) 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, a, b; cin >> n; int ind[100005]; for (int i = 1; i < n; i++) { cin >> a >> b; ind[a]++; ind[b]++; } for (int i = 1; i <= n; i++) { if (ind[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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<int> v[100002]; int n, x, y; int main() { cin >> n; for (int i = 1; i < n; i++) { cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } for (int i = 1; i <= n; i++) { if (v[i].size() % 2 == 0) { 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" ] }
IN-CORRECT
java
import java.util.*; import java.lang.*; import java.io.*; public class Main { PrintWriter out = new PrintWriter(System.out); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(""); String next() throws IOException { if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int ni() throws IOException { return Integer.parseInt(next()); } long nl() throws IOException { return Long.parseLong(next()); } long mod=1000000007; int n; ArrayList<Integer>[]A; boolean f; boolean[]L; void solve() throws IOException { n=ni(); A=new ArrayList[n+1]; for (int i=1;i<=n;i++) A[i]=new ArrayList(); for (int i=1;i<n;i++) { int u=ni(),v=ni(); A[u].add(v); A[v].add(u); } f=true; L=new boolean[n+1]; int root=0; for (int i=1;i<=n;i++) { if (A[i].size()>1) { root=i; break; } } if (root==0) out.println("YES"); else if (n==3) out.println("NO"); else { dfs(root,0); if (f) out.println("YES"); else out.println("NO"); } out.flush(); } void dfs(int u,int p) { int lc=0; if (A[u].size()==1) { L[u]=true; return; } for (int v:A[u]) { if (v==p) continue; dfs(v,u); if (L[v]) lc++; } if (lc==1 && A[u].size()==2) f=false; } int gcd(int a,int b) { return(b==0?a:gcd(b,a%b)); } long gcd(long a,long b) { return(b==0?a:gcd(b,a%b)); } long mp(long a,long p) { long r=1; while(p>0) { if ((p&1)==1) r=(r*a)%mod; p>>=1; a=(a*a)%mod; } return r; } public static void main(String[] args) throws IOException { new Main().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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse4") const long double EPS = 1e-9; const long long INF = 1e9; const long long mod = 1e9 + 7; using namespace std; mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); long long n, m, k; long long q; vector<vector<long long>> vec; vector<bool> used; vector<long long> cnt; vector<long long> ans; vector<long long> pref; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n; vec.resize(n); cnt.resize(n, 0); for (int i = 0; i < n - 1; ++i) { int v, to; cin >> v >> to; --v; --to; vec[v].push_back(to); vec[to].push_back(v); cnt[v]++; cnt[to]++; } int cntl = 0, cntnl = 0; for (int i = 0; i < n; ++i) { if (cnt[i] == 1) cntl++; else { int cntal = 0; for (int j = 0; j < vec[i].size(); ++j) { if (cnt[vec[i][j]] == 1) { cntal++; } } if (cntal <= 1) { cntnl++; } } } if ((cntl == 2 && n > 2) || cntnl || cntl % 2) { cout << "NO"; } else { 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" ] }
IN-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); } if(n==2) { out.println("YES"); return ; } boolean[] jud=new boolean[n+1]; for(int i=1;i<=n;i++) { if(map[i].size()==0) 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct EdgeList { int x, y, val; }; int n, q, p, u, v, top; vector<vector<int> > a; EdgeList b[100000], res[200000]; 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, val; cin >> x >> y >> val; x--; y--; a[x].push_back(y); a[y].push_back(x); b[i].x = x; b[i].y = y; b[i].val = val; } } void Bfs1(int t, int ne) { if (a[t].size() == 1) { q = t; return; } queue<pair<int, int> > o; for (int i = 0; i <= a[t].size() - 1; i++) if (a[t][i] != ne) o.push({a[t][i], i}); int vis[20000]; memset(vis, -1, sizeof(vis)); int pos = -1; while (o.size() != 0) { pair<int, int> c = o.front(); o.pop(); int k = c.first; if (a[k].size() == 1) { if (q != -1 && c.second != pos) { p = k; return; } else { q = k; pos = c.second; } } for (int i = 0; i <= a[k].size() - 1; i++) if (vis[a[k][i]] == -1 && a[k][i] != ne) { vis[a[k][i]] = 1; o.push({a[k][i], c.second}); } } } void Bfs2(int t, int ne) { if (a[t].size() == 1) { u = t; return; } queue<pair<int, int> > o; for (int i = 0; i <= a[t].size() - 1; i++) if (a[t][i] != ne) o.push({a[t][i], i}); int vis[20000]; memset(vis, -1, sizeof(vis)); int pos = -1; while (o.size() != 0) { pair<int, int> c = o.front(); o.pop(); int k = c.first; if (a[k].size() == 1) { if (u != -1 && c.second != pos) { v = k; return; } else { u = k; pos = c.second; } } for (int i = 0; i <= a[k].size() - 1; i++) if (vis[a[k][i]] == -1 && a[k][i] != ne) { vis[a[k][i]] = 1; o.push({a[k][i], c.second}); } } } void Process() { for (int i = 0; i <= n - 1; i++) if (a[i].size() == 2) { cout << "NO"; return; } for (int i = 1; i <= n - 1; i++) { q = p = u = v = -1; Bfs1(b[i].x, b[i].y); Bfs2(b[i].y, b[i].x); if (p == -1) { swap(q, u); swap(p, v); } if (v == -1 && p != -1) { top++; res[top].x = u; res[top].y = q; res[top].val = b[i].val / 2; top++; res[top].x = p; res[top].y = q; res[top].val = -(b[i].val / 2); top++; res[top].x = u; res[top].y = p; res[top].val = b[i].val / 2; } else if (v == -1 && p == -1) { top++; res[top].x = u; res[top].y = q; res[top].val = b[i].val; } else { top++; res[top].x = u; res[top].y = q; res[top].val = b[i].val / 2; top++; res[top].x = u; res[top].y = v; res[top].val = -(b[i].val / 2); top++; res[top].x = q; res[top].y = p; res[top].val = -(b[i].val / 2); top++; res[top].x = v; res[top].y = p; res[top].val = b[i].val / 2; } } cout << "YES" << endl << top << endl; for (int i = 1; i <= top; i++) cout << res[i].x + 1 << " " << res[i].y + 1 << " " << res[i].val << endl; } int main() { Input(); Process(); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long maxN = 2 * 100224; struct BIT { long long data[maxN] = {0}; void update(long long idx, long long val) { while (idx < maxN) { data[idx] += val; idx += idx & -idx; } } void update(long long l, long long r, long long val) { update(l, val); update(r + 1, -val); } long long query(long long idx) { long long res = 0; while (idx > 0) { res += data[idx]; idx -= idx & -idx; } return res; } long long query(long long l, long long r) { return query(r) - query(l); } }; struct LazyBIT { BIT bitAdd, bitSub; void update(long long l, long long r, long long val) { bitAdd.update(l, r, val); bitSub.update(l, r, (l - 1) * val); bitSub.update(r + 1, (-r + l - 1) * val); } long long query(long long idx) { return idx * bitAdd.query(idx) - bitSub.query(idx); } long long query(long long l, long long r) { return query(r) - query(l - 1); } }; long long parent[maxN]; long long rnk[maxN]; long long lfmost[maxN]; long long rtmost[maxN]; long long vis[maxN]; void make_set(long long v) { parent[v] = v; rnk[v] = 0; lfmost[v] = v; rtmost[v] = v; } long long find_set(long long v) { if (v == parent[v]) return v; return parent[v] = find_set(parent[v]); } void union_sets(long long a, long long b) { a = find_set(a); b = find_set(b); if (a != b) { if (rnk[a] < rnk[b]) swap(a, b); parent[b] = a; lfmost[a] = min(lfmost[a], lfmost[b]); rtmost[a] = max(rtmost[a], rtmost[b]); if (rnk[a] == rnk[b]) rnk[a]++; } } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); long long n; cin >> n; vector<long long> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } long long sum = 0; LazyBIT B; for (long long i = 1; i <= n; i++) { B.update(i, i, sum); sum += i; } vector<long long> haha; for (int i = n - 1; i >= 0; i--) { long long lo = 1; long long hi = n; long long mid; long long ans; long long val; long long temp; while (lo <= hi) { mid = (lo + hi) / 2; if (vis[mid] == 1) { if (lfmost[find_set(mid)] - 1 >= lo) mid = lfmost[find_set(mid)] - 1; else if (rtmost[find_set(mid)] + 1 <= hi) mid = rtmost[find_set(mid)] + 1; else break; } val = B.query(mid, mid); if (val == v[i]) { ans = mid; hi = mid - 1; } else if (v[i] > val) { lo = mid + 1; } else if (v[i] < val) { hi = mid - 1; } } B.update(ans, n, -ans); vis[ans] = 1; make_set(ans); if (vis[ans] == 1 && vis[ans + 1] == 1) union_sets(ans, ans + 1); if (vis[ans] == 1 && vis[ans - 1] == 1) union_sets(ans, ans - 1); haha.push_back(ans); } reverse(haha.begin(), haha.end()); for (auto u : haha) { cout << u << " "; } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int const maxn = 1e6 + 5; long long tree[4 * maxn], lazy[4 * maxn], a[maxn]; int kq[maxn]; int n; void down(int g) { if (lazy[g] != 0) { tree[g << 1] += lazy[g]; lazy[g << 1] += lazy[g]; tree[(g << 1) | 1] += lazy[g]; lazy[(g << 1) | 1] += lazy[g]; lazy[g] = 0; } } void buld(int g, int l, int r) { if (l == r) { tree[g] = a[l]; } else { int m = (l + r) >> 1; buld(g << 1, l, m); buld((g << 1) | 1, m + 1, r); tree[g] = min(tree[g << 1], tree[(g << 1) | 1]); } } void update(int g, int l, int r, int u, int v, long long val) { if (r < u || v < l) return; if (u <= l && r <= v) { tree[g] += val; lazy[g] += val; } else { down(g); int m = (l + r) >> 1; update(g << 1, l, m, u, v, val); update((g << 1) | 1, m + 1, r, u, v, val); tree[g] = min(tree[g << 1], tree[(g << 1) | 1]); } } int find_zero(int g, int l, int r) { if (l == r) return l; else { down(g); int m = (l + r) >> 1; int res = -1; if (tree[(g << 1) | 1] == 0) res = find_zero((g << 1) | 1, m + 1, r); else res = find_zero(g << 1, l, m); tree[g] = min(tree[g << 1], tree[(g << 1) | 1]); return res; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; buld(1, 1, n); for (int i = 1; i <= n; i++) { int pos = find_zero(1, 1, n); kq[pos] = i; update(1, 1, n, pos, pos, 1e18); update(1, 1, n, pos + 1, n, -i); } for (int i = 1; i <= n; i++) cout << kq[i] << " "; return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, a[500001], f[500001], ans[500001], mn; int lowbit(int x) { return x & (-x); } void ins(long long x, long long w) { long long i; for (i = x; i <= n; i += lowbit(i)) f[i] += w; } long long query(long long x) { long long xlh = 0, i; for (i = x; i; i -= lowbit(i)) xlh += f[i]; return xlh; } int main() { long long i, l, r, mid; scanf("%lld", &n); for (i = 1; i <= n; i++) scanf("%lld", &a[i]); for (i = 1; i <= n; i++) ins(i, i); for (i = n; i; i--) { l = 1; r = n; mn = 1; while (l <= r) { mid = (l + r) / 2; if (query(mid - 1) <= a[i]) mn = mid, l = mid + 1; else r = mid - 1; } ins(mn, -mn); ans[i] = mn; } for (i = 1; i <= n; i++) printf("%lld ", ans[i]); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
python2
def bsearch(k,tree,n): low = 1 high = n cnt = 0 while low <= high: mid = (low+high)/2 tmp1 = sum(tree,mid-1) tmp2 = sum(tree,mid) s = mid*(mid-1)/2 if k+tmp2 == s and tmp1 == tmp2: break elif k+tmp1 <s: high = mid-1 else: low = mid +1 return mid def lowbit(x): return x&-x def add(t,i,v): while i<len(t): t[i]+=v i += lowbit(i) def sum(t,i): ans = 0 while i>0: ans += t[i] i -= lowbit(i) return ans def main(): n = int(input()) arr = readnumbers() tree = [0]*(n+1) ans = [] dic = [0]*(n+1) for i in range(n-1): v = bsearch(arr[n-1-i],tree,n) dic[v] = 1 ans.append(v) add(tree,v,v) for k,v in enumerate(dic[1:]): if v == 0: ans.append(k+1) break print " ".join(map(str,ans[::-1])) ######## Python 2 and 3 footer by Pajenegod and c1729 # Note because cf runs old PyPy3 version which doesn't have the sped up # unicode strings, PyPy3 strings will many times be slower than pypy2. # There is a way to get around this by using binary strings in PyPy3 # but its syntax is different which makes it kind of a mess to use. # So on cf, use PyPy2 for best string performance. py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange import os, sys from io import IOBase, BytesIO BUFSIZE = 8192 class FastIO(BytesIO): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.writable = "x" in file.mode or "w" in file.mode self.write = super(FastIO, self).write if self.writable else None def _fill(self): s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0]) return s def read(self): while self._fill(): pass return super(FastIO,self).read() def readline(self): while self.newlines == 0: s = self._fill(); self.newlines = s.count(b"\n") + (not s) self.newlines -= 1 return super(FastIO, self).readline() def flush(self): if self.writable: os.write(self._fd, self.getvalue()) self.truncate(0), self.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable if py2: self.write = self.buffer.write self.read = self.buffer.read self.readline = self.buffer.readline else: 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') sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip('\r\n') # Cout implemented in Python import sys class ostream: def __lshift__(self,a): sys.stdout.write(str(a)) return self cout = ostream() endl = '\n' # Read all remaining integers in stdin, type is given by optional argument, this is fast def readnumbers(zero = 0): conv = ord if py2 else lambda x:x A = []; numb = zero; sign = 1; i = 0; s = sys.stdin.buffer.read() try: while True: if s[i] >= b'0' [0]: numb = 10 * numb + conv(s[i]) - 48 elif s[i] == b'-' [0]: sign = -1 elif s[i] != b'\r' [0]: A.append(sign*numb) numb = zero; sign = 1 i += 1 except:pass if s and s[-1] >= b'0' [0]: A.append(sign*numb) return A if __name__== "__main__": main()
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.util.*; import java.io.*; import java.math.*; public class Solution{ public static long[] bit; public static int n; public static long ver(int c,long sum){ long r =(long) c*(long)(c-1); r/=(long)2; return sum-r; } public static void update(int x,long val){ for(;x<=n;x+=x&-x) bit[x] += val; } public static long query(int x){ long sum=0; for(;x>0;x-=x&-x) sum += bit[x]; return sum; } public static void main(String[] args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(br.readLine()); n = Integer.parseInt(st.nextToken()); long[] s = new long[n+1]; st = new StringTokenizer(br.readLine()); for(int i=1;i<=n;i++) s[i] = Long.parseLong(st.nextToken()); int[] p = new int[n+1]; bit = new long[n+1]; for(int i=n;i>0;i--){ int l = 1; int r = n+1; while(l<r){ if(l==r-1) break; int c = l+r; c /= 2; if(ver(c,s[i]+query(c-1))<0){//sum from 1 to < c r = c; }else l = c; } p[i] = l; update(p[i],(long)p[i]); } out.print(p[1]); for(int i=2;i<=n;i++) out.print(" "+p[i]); out.println(""); out.flush(); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long int ar[4 * 200005]; void update(int a, int b, int l, int h, int p) { if (a < l || a > h) return; if (l == h) { ar[p] += b; return; } int m = (l + h) / 2; update(a, b, l, m, 2 * p); update(a, b, m + 1, h, 2 * p + 1); ar[p] += b; return; } int query(int l, int h, int p, long long int s) { if (l == h) return l; int m = (l + h) / 2; if (ar[2 * p] > s) return query(l, m, 2 * p, s); else return query(m + 1, h, 2 * p + 1, s - ar[2 * p]); } int compute(long long int s) { int x; s *= 2; x = sqrt(s); return x + 1; } int main() { int i, n; scanf("%d", &n); vector<long long int> sum(n); for (i = 0; i < n; i++) cin >> sum[i]; vector<int> ans(n); ans[n - 1] = compute(sum[n - 1]); for (i = 1; i < n + 1; i++) update(i, i, 0, n, 1); update(ans[n - 1], -ans[n - 1], 0, n, 1); for (i = n - 2; i >= 0; i--) { ans[i] = query(0, n, 1, sum[i]); update(ans[i], -ans[i], 0, n, 1); } for (i = 0; i < n; i++) printf("%d ", ans[i]); printf("\n"); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.*; import java.util.*; public class D { public static class segtree { long[] t; long[] lazy; int n; /* USAGE * Template is segtree, lazy prop w/ addition. * For assignment updates (a[k] = u), create new boolean array. Recurrence becomes assigning children to yourself. * Let lazy be the boolean 'marked' array. * Delete lazy & push if TLE, saves about ~100ms. */ void update(int l, int r, long a) {update(1, 0, n-1, l, r, a);} long query(int l, int r) {return query(1,0,n-1,l,r);} public segtree(long[] a) { n=a.length; t = new long[n*4]; lazy = new long[n*4]; build(a, 1, 0, a.length-1); } void build(long[] a, int v, int tl, int tr) { if (tl == tr) { t[v] = a[tl]; return; } int tm = (tl+tr)/2; build(a, v*2, tl, tm); build(a, v*2+1, tm+1, tr); t[v] = Long.min(t[v*2], t[v*2+1]); // Addition, change to to fit whatever ops. } void push(int v) { if (lazy[v] == 0) return; t[v*2] += lazy[v]; lazy[v*2] += lazy[v]; t[v*2+1] += lazy[v]; lazy[v*2+1] += lazy[v]; lazy[v] = 0; } void update(int v, int tl, int tr, int l, int r, long new_val) { if (l > r) return; if (l == tl && r == tr) { t[v] += new_val; lazy[v] += new_val; } else { push(v); int tm = (tl+tr)/2; update(v*2, tl, tm, l, Integer.min(r, tm), new_val); update(v*2+1, tm+1,tr, Integer.max(l, tm+1),r, new_val); t[v] = Long.min(t[v*2] , t[v*2+1]); } } long query(int v, int tl, int tr, int l, int r) { if (l > r) return Long.MAX_VALUE; if (l == tl && tr == r) return t[v]; push(v); int tm = (tl+tr)/2; return Long.min(query(v*2, tl, tm, l, Integer.min(r, tm)) , query(v*2+1, tm+1, tr, Integer.max(l, tm+1), r)); } } public static Reader sc = new Reader(); //public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { //BufferedReader br = new BufferedReader(new FileReader("input.in")); int n = sc.nextInt(); long[] a= new long[n]; for (int i = 0; i < n; i++) a[i] = sc.nextLong(); segtree s = new segtree(a); long[] res = new long[n]; for (int i = 1; i <= n; i++) { int l = -1; int r = n-1; while (l+1<r) { int mid = (l+r)/2; if (s.query(mid, r) == 0) { l = mid; } else r = mid; } //System.out.println(l + " " + r); int next = 0; if (s.query(r, r) == 0) { res[r] = i; next = r+1; if (next < n) { s.update(next, n-1,-i); } s.update(r, r, Long.MAX_VALUE); } else if (l >= 0){ res[l] = i; next = l+1; if (next < n) { s.update(next, n-1,-i); } s.update(l, l, Long.MAX_VALUE); } } for (long x: res) { out.print(x + " "); } out.println(); out.close(); } static long ceil(long a, long b) { return (a + b - 1) / b; } static long powMod(long base, long exp, long mod) { if (base == 0 || base == 1) return base; if (exp == 0) return 1; if (exp == 1) return base % mod; long R = powMod(base, exp / 2, mod) % mod; R *= R; R %= mod; if ((exp & 1) == 1) { return base * R % mod; } else return R % mod; } static long pow(long base, long exp) { if (base == 0 || base == 1) return base; if (exp == 0) return 1; if (exp == 1) return base; long R = pow(base, exp / 2); if ((exp & 1) == 1) { return R * R * base; } else return R * R; } 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[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { break; } buf[cnt++] = (byte) c; } return new String(buf, 0, Integer.max(cnt - 1, 0)); } 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(); } } public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MAX = 200100; pair<long long, long long> tree[256 * 1024 * 2]; long long lazy[256 * 1024 * 2]; void updateRangeUtil(long long si, long long ss, long long se, long long us, long long ue, long long diff) { if (lazy[si] != 0) { tree[si].first += lazy[si]; if (ss != se) { lazy[si * 2 + 1] += lazy[si]; lazy[si * 2 + 2] += lazy[si]; } lazy[si] = 0; } if (ss > se || ss > ue || se < us) return; if (ss >= us && se <= ue) { tree[si].first += diff; if (ss != se) { lazy[si * 2 + 1] += diff; lazy[si * 2 + 2] += diff; } return; } long long mid = (ss + se) / 2; updateRangeUtil(si * 2 + 1, ss, mid, us, ue, diff); updateRangeUtil(si * 2 + 2, mid + 1, se, us, ue, diff); tree[si] = min(tree[si * 2 + 1], tree[si * 2 + 2]); } void updateRange(long long n, long long us, long long ue, long long diff) { updateRangeUtil(0, 0, n - 1, us, ue, diff); } void updateRangeUtil2(long long si, long long ss, long long se, long long us, long long ue, long long diff) { if (ss > se || ss > ue || se < us) return; if (ss >= us && se <= ue) { tree[si].second += diff; return; } long long mid = (ss + se) / 2; updateRangeUtil2(si * 2 + 1, ss, mid, us, ue, diff); updateRangeUtil2(si * 2 + 2, mid + 1, se, us, ue, diff); tree[si] = min(tree[si * 2 + 1], tree[si * 2 + 2]); } void updateRange2(long long n, long long us, long long ue, long long diff) { updateRangeUtil2(0, 0, n - 1, us, ue, diff); } int main() { long long arr[MAX]; long long n; cin >> n; for (long long i = 0; i < n; ++i) { cin >> arr[i]; } reverse(arr, arr + n); for (long long i = 0; i < n; ++i) { updateRange2(n, i, i, i); } for (long long i = 0; i < n; ++i) { updateRange(n, i, i, arr[i]); } long long res[MAX]; for (long long iter = 1; iter <= n; ++iter) { long long smallest = tree[0].second; updateRange(n, 0, smallest, -iter); updateRange(n, smallest, smallest, 1LL << 62); res[smallest] = iter; } reverse(res, res + n); for (long long i = 0; i < n; ++i) { if (i) cout << ' '; cout << res[i]; } cout << endl; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
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.TreeSet; import java.io.InputStream; /** * Built using CHelper plug-in Actual solution is at the top * * @author ilyakor */ 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); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); long[] s = new long[n]; for (int i = 0; i < n; ++i) { s[i] = in.nextLong(); } long[] a = new long[n + 1]; TreeSet<Integer> alive = new TreeSet<>(); for (int i = 1; i <= n; ++i) { a[i] = i; alive.add(i); } TaskD.FenwickTree tree = new TaskD.FenwickTree(a); int[] res = new int[n]; for (int i = n - 1; i >= 0; --i) { int val = tree.lower_bound(s[i]) + 1; val = alive.ceiling(val); res[i] = val; tree.set(val, 0); alive.remove(val); a[val] = 0; } for (int i = 0; i < n; ++i) { out.print(res[i] + " "); } } static class FenwickTree { long[] t; int n; public FenwickTree(int n) { this.n = n; t = new long[n]; } public FenwickTree(long[] a) { n = a.length; t = new long[n]; for (int i = 0; i < n; i++) { t[i] += a[i]; int j = i | i + 1; if (j < n) { t[j] += t[i]; } } } public void add(int i, long value) { for (; i < n; i += (i + 1) & -(i + 1)) { t[i] += value; } } public long get(int i) { // return sum(i) - sum(i - 1); long res = t[i]; if (i > 0) { int lca = i - ((i + 1) & -(i + 1)); --i; while (i != lca) { res -= t[i]; i -= (i + 1) & -(i + 1); } } return res; } public void set(int i, long value) { add(i, -get(i) + value); } public int lower_bound(long sum) { --sum; int pos = -1; for (int blockSize = Integer.highestOneBit(n); blockSize != 0; blockSize >>= 1) { int nextPos = pos + blockSize; if (nextPos < n && sum >= t[nextPos]) { sum -= t[nextPos]; pos = nextPos; } } return pos + 1; } } } 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(); } } static class InputReader { private InputStream stream; private byte[] buffer = new byte[10000]; private int cur; private int count; public InputReader(InputStream stream) { this.stream = stream; } public static boolean isSpace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int read() { if (count == -1) { throw new InputMismatchException(); } try { if (cur >= count) { cur = 0; count = stream.read(buffer); if (count <= 0) { return -1; } } } catch (IOException e) { throw new InputMismatchException(); } return buffer[cur++]; } public int readSkipSpace() { int c; do { c = read(); } while (isSpace(c)); return c; } public int nextInt() { int sgn = 1; int c = readSkipSpace(); if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res = res * 10 + c - '0'; c = read(); } while (!isSpace(c)); res *= sgn; return res; } public long nextLong() { long sgn = 1; int c = readSkipSpace(); if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res = res * 10L + (long) (c - '0'); c = read(); } while (!isSpace(c)); res *= sgn; return res; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
python3
from sys import stdin,stdout class Tree(object): def __init__(self,n): self.tree=[0]*(4*n+10) self.b=[0]*(n+10) self.a=list(map(int,stdin.readline().split())) self.n=n def update(self,L,C,l,r,rt): if l==r: self.tree[rt]+=C return mid=(l+r)//2 if L<=mid: self.update(L,C,l,mid,rt<<1) else: self.update(L,C,mid+1,r,rt<<1|1) self.tree[rt]=self.tree[rt<<1]+self.tree[rt<<1|1] def query(self,s,l,r,rt): if l==r: return l mid=(l+r)//2 if self.tree[rt<<1]>s: return self.query(s,l,mid,rt<<1) else: return self.query(s-self.tree[rt<<1],mid+1,r,rt<<1|1) def slove(self): for i in range(n): self.update(i+1,i+1,1,n,1) for i in range(n,0,-1): self.b[i]=self.query(self.a[i-1],1,n,1) self.update(self.b[i],-self.b[i],1,n,1) for i in range(n): stdout.write('%d '%(self.b[i+1])) if __name__ == '__main__': n=int(stdin.readline()) seg=Tree(n) seg.slove()
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.*; import java.util.*; public class Test { static int readInt() { int ans = 0; boolean neg = false; try { boolean start = false; for (int c = 0; (c = System.in.read()) != -1; ) { if (c == '-') { start = true; neg = true; continue; } else if (c >= '0' && c <= '9') { start = true; ans = ans * 10 + c - '0'; } else if (start) break; } } catch (IOException e) { } return neg ? -ans : ans; } static long readLong() { long ans = 0; boolean neg = false; try { boolean start = false; for (int c = 0; (c = System.in.read()) != -1; ) { if (c == '-') { start = true; neg = true; continue; } else if (c >= '0' && c <= '9') { start = true; ans = ans * 10 + c - '0'; } else if (start) break; } } catch (IOException e) { } return neg ? -ans : ans; } static String readLine() { StringBuilder b = new StringBuilder(); try { boolean start = false; for (int c = 0; (c = System.in.read()) != -1; ) { if (Character.isLetterOrDigit(c)) { start = true; b.append((char)c); } else if (start) break; } } catch (IOException e) { } return b.toString(); } static PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); long[] f = new long[234567]; long[] s = new long[234567]; int n; void add(int i, long v) { while (i <= n) { f[i] += v; i += (i & (-i)); } } long sum(int i) { long sum = 0; while (i > 0) { sum += f[i]; i -= (i & (-i)); } return sum; } void start() { n = readInt(); for (int i = 1; i <= n; i++) s[i] = readLong(); for (int i = 1; i <= n; i++) add(i, i); int[] ans = new int[n+1]; for (int i = n; i >= 1; i--) { int ll = 1, rr = n; while (ll < rr) { int mid = (rr + ll) / 2; long a = sum(mid); if (a > s[i]) rr = mid; else ll = mid + 1; } ans[i] = ll; add(ll, -ll); } for (int i = 1; i <= n; i++) writer.print(ans[i] + " "); } public static void main(String[] args) { Test te = new Test(); te.start(); writer.flush(); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
python2
import sys sys.setrecursionlimit(10**7) rr = raw_input rri = lambda: int(raw_input()) rrm = lambda: map(int, raw_input().split()) class Fenwick: def __init__(self, N): self.A = [0] * (N+1) def update(self, x, delta): while x < len(self.A): self.A[x] += delta x += x & -x def query(self, x): ans = 0 while x > 0: ans += self.A[x] x -= x & -x return ans def lift(self, top): x = su = 0 for i in xrange(21, -1, -1): y = x + (1 << i) if y < len(self.A) and su + self.A[y] <= top: x += 1 << i su += self.A[x] return x + 1 def solve(N, A): fenw = Fenwick(N) ans = [0] * N for i in xrange(1, N+1): fenw.update(i, i) for i in xrange(N-1, -1, -1): ans[i] = fenw.lift(A[i]) fenw.update(ans[i], -ans[i]) return ans print " ".join(map(str, solve(rri(), rrm())))
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.DataInputStream; import java.io.IOException; import java.io.PrintWriter; public class P1208D4 { public static void main(String[] args) throws IOException { InputReader2 ir = new InputReader2(); PrintWriter pw = new PrintWriter(System.out); int n = ir.nextInt(); long[] s = new long[n]; for (int i = 0; i < n; i++) { s[i] = ir.nextLong(); } BIT tree = new BIT(n); for (int i = 1; i <= n; i++) { tree.add(i, i); } int[] ans = new int[n]; for (int i = n - 1; i >= 0; i--) { long cur = s[i]; if (cur == 0) { ans[i] = binSearch(tree, 0, n); } else { ans[i] = binSearch(tree, cur, n); } tree.remove(ans[i]); } for (int an : ans) { pw.print(an + " "); } pw.close(); } private static int binSearch(BIT tree, long key, int n) { int low = 1, high = n; while (low <= high) { int mid = (low + high) / 2; long amt = tree.prefixSum(mid); if (amt <= key) { low = mid + 1; } else { high = mid - 1; } } return low; } public static class BIT { final int N; private final long[] tree; public BIT(int sz) { tree = new long[(N = sz + 1)]; } private static int lsb(int i) { return i & -i; } private long prefixSum(int i) { long sum = 0L; while (i != 0) { sum += tree[i]; i &= ~lsb(i); } return sum; } public void add(int i, long v) { while (i < N) { tree[i] += v; i += lsb(i); } } public void remove(int i) { add(i, -(prefixSum(i) - prefixSum(i - 1))); } } private static class InputReader2 { final private int BUFFER_SIZE = 1 << 16; private final DataInputStream dis; private final byte[] buffer; private int bufferPointer, bytesRead; public InputReader2() { dis = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); return ret; } private long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); return ret; } private void fillBuffer() throws IOException { bytesRead = dis.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.*; import java.util.StringTokenizer; public class C { static int find(int i, int[] nxt) { return nxt[i] = i == nxt[i] ? i : find(nxt[i], nxt); } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); long[] base = new long[n + 1]; long s = 0; int[] nxt = new int[n + 1]; for (int i = 1; i <= n; ++i) { base[i] = s; s += i; nxt[i] = i; } long[] a = new long[n]; for(int i = 0; i < n; ++i) { a[i] = sc.nextLong(); } FenwickTree ft = new FenwickTree(n); int[] ans = new int[n]; for(int i = n - 1; i >= 0; --i) { int res = -1, lo = 1, hi = n; while(lo <= hi) { int mid = (lo + hi) / 2; long t = base[mid] - ft.query(mid); if (t >= a[i]) { res = mid; hi = mid - 1; } else lo = mid + 1; } ans[i] = find(res, nxt); ft.update(ans[i] + 1, ans[i]); nxt[ans[i]]++; } for(int x: ans) out.print(x + " "); out.close(); } static class FenwickTree { long[] ft; FenwickTree(int n) { ft = new long[n + 2]; } void update(int l, int v) { while(l < ft.length) { ft[l] += v; l += l & -l; } } long query(int i) { long s = 0; while(i > 0) { s += ft[i]; i ^= i & -i; } return s; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public boolean ready() throws IOException {return br.ready();} } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; const int maxm = 4e6 + 10; const long long mod = 1e9 + 7; const int inf = 0x3ffffff; const double eps = 1e-5; long long n, s[maxn]; long long a[maxn], ans[maxn << 2], tag[maxn << 2], m; long long res[maxn]; inline void push_up(long long p) { ans[p] = ans[p << 1] + ans[p << 1 | 1]; } inline void f(long long p, long long l, long long r, long long k) { tag[p] += k; ans[p] += (r - l + 1) * k; } void build(long long l, long long r, long long p) { if (l == r) { ans[p] = a[l]; return; } long long mid = (l + r) >> 1; build(l, mid, p << 1); build(mid + 1, r, p << 1 | 1); push_up(p); } inline void push_down(long long p, long long l, long long r) { long long mid = (l + r) >> 1; f(p << 1, l, mid, tag[p]); f(p << 1 | 1, mid + 1, r, tag[p]); tag[p] = 0; } inline void update(long long nl, long long nr, long long l, long long r, long long p, long long k) { if (nl <= l && r <= nr) { ans[p] += k * (r - l + 1); tag[p] += k; return; } push_down(p, l, r); long long mid = (l + r) >> 1; if (nl <= mid) update(nl, nr, l, mid, p << 1, k); if (nr > mid) update(nl, nr, mid + 1, r, p << 1 | 1, k); push_up(p); } long long query(long long q_x, long long q_y, long long l, long long r, long long p) { long long res = 0; if (q_x <= l && q_y >= r) return ans[p]; long long mid = (l + r) >> 1; push_down(p, l, r); if (q_x <= mid) res += query(q_x, q_y, l, mid, p << 1); if (q_y > mid) res += query(q_x, q_y, mid + 1, r, p << 1 | 1); return res; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> s[i]; a[i] = i; } build(1, n, 1); for (int i = n; i >= 1; i--) { int l = 1, r = n; while (l < r) { int mid = (l + r) >> 1; if (query(1, mid, 1, n, 1) > s[i]) r = mid; else l = mid + 1; } update(l, l, 1, n, 1, -l); res[i] = l; } for (int i = 1; i <= n; i++) cout << res[i] << " "; return ~~(0 - 0); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
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.util.InputMismatchException; import java.io.IOException; import java.util.TreeSet; import java.util.ArrayList; import java.util.List; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.Collections; 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 in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.i(); long[] a = in.la(n); TreeSet<Integer> set = new TreeSet<>(); Fenwick f = new Fenwick(n + 5); for (int i = 1; i <= n; i++) { set.add(i); f.add(i, i); } List<Integer> list = new ArrayList<>(); for (int i = n - 1; i >= 0; i--) { int ind = f.indexWithGivenCumFreq(a[i]); Integer u = set.ceiling(ind); f.add(u, -u); list.add(u); set.remove(u); } Collections.reverse(list); list.forEach(e -> out.print(e + " ")); } } 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(); } } static class InputReader { private InputStream is; private byte[] inbuf = new byte[1024]; private int lenbuf = 0; private int ptrbuf = 0; public InputReader(InputStream is) { this.is = is; } 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++]; } public long[] la(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = l(); return a; } public int i() { 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(); } } public long l() { 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(); } } } static class Fenwick { public final long[] bit; public final int size; public Fenwick(int[] a) { this(a.length); for (int i = 0; i < a.length; i++) { this.add(i, a[i]); } } public Fenwick(long[] a) { this(a.length); for (int i = 0; i < a.length; i++) this.add(i, a[i]); } public Fenwick(int size) { bit = new long[size + 1]; this.size = size + 1; } public void add(int i, long delta) { for (++i; i < size; i += (i & -i)) { bit[i] += delta; } } public int indexWithGivenCumFreq(long v) { int i = 0, n = size; for (int b = Integer.highestOneBit(n); b != 0; b >>= 1) { if ((i | b) < n && bit[i | b] <= v) { i |= b; v -= bit[i]; } } return i; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; ll P[200001]; ll sum[200001]; ll fen[200001]; int ans[200001]; void build() { for (int i = 1; i <= 200000; i++) { sum[i] = sum[i - 1] + ll(i - 1); fen[i] = sum[i] - sum[i - (i & -i)]; } } ll query(int l, int r) { if (l != 1LL) return query(1, r) - query(1, l - 1); ll ans = 0LL; while (r > 0) { ans += fen[r]; r -= r & -r; } return ans; } void update(int i) { ll taking = ll(i); i++; while (i <= 200000) { fen[i] -= taking; i += (i & -i); } } int find(ll s) { ll cur = 0LL; int ind = 0; for (int b = 17; b >= 0; b--) { if (ind + (1 << b) <= 200000 && cur + fen[ind + (1 << b)] <= s) { cur += fen[ind + (1 << b)]; ind += (1 << b); } } return ind; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> P[i]; build(); for (int i = n; i >= 1; i--) { ans[i] = find(P[i]); update(ans[i]); } for (int i = 1; i <= n; i++) { cout << ans[i] << " "; } cout << endl; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long read() { char ch = getchar(); long long x = 0, ff = 1; while (ch < '0' || ch > '9') { if (ch == '-') ff = -ff; ch = getchar(); } while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * ff; } void write(long long aa) { if (aa < 0) putchar('-'), aa = -aa; if (aa > 9) write(aa / 10); putchar(aa % 10 + '0'); return; } long long n; long long a[200005]; long long tt[800005], tts[800005], lz[800005]; void up(long long rt) { tt[rt] = min(tt[rt << 1], tt[rt << 1 | 1]); if (tt[rt << 1] >= tt[rt << 1 | 1]) tts[rt] = tts[rt << 1 | 1]; else tts[rt] = tts[rt << 1]; return; } void bt(long long rt, long long ll, long long rr) { if (ll == rr) { tt[rt] = read(), tts[rt] = ll; return; } long long mid = (ll + rr) >> 1; bt(rt << 1, ll, mid); bt(rt << 1 | 1, mid + 1, rr); up(rt); return; } void push(long long rt) { if (!lz[rt]) return; tt[rt << 1] += lz[rt]; tt[rt << 1 | 1] += lz[rt]; lz[rt << 1] += lz[rt]; lz[rt << 1 | 1] += lz[rt]; lz[rt] = 0; return; } void update(long long rt, long long ll, long long rr, long long L, long long R, long long kk) { if (ll == L && rr == R) { tt[rt] += kk; lz[rt] += kk; return; } long long mid = (ll + rr) >> 1; push(rt); if (R <= mid) update(rt << 1, ll, mid, L, R, kk); else if (L > mid) update(rt << 1 | 1, mid + 1, rr, L, R, kk); else update(rt << 1, ll, mid, L, mid, kk), update(rt << 1 | 1, mid + 1, rr, mid + 1, R, kk); up(rt); return; } int main() { n = read(); bt(1, 1, n); for (long long i = 1; i <= n; ++i) { long long now = tts[1]; a[now] = i; update(1, 1, n, now, now, n * n); if (now < n) update(1, 1, n, now + 1, n, -i); } for (long long i = 1; i <= n; ++i, putchar(' ')) write(a[i]); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int M = 2e5 + 5; int p[M << 2], res[M]; long long V[M << 2], tag[M << 2]; void push_up(int o) { if (V[o << 1] < V[o << 1 | 1]) { V[o] = V[o << 1]; p[o] = p[o << 1]; } else { V[o] = V[o << 1 | 1]; p[o] = p[o << 1 | 1]; } } void push_down(int o) { tag[o << 1] += tag[o]; tag[o << 1 | 1] += tag[o]; V[o << 1] += tag[o]; V[o << 1 | 1] += tag[o]; tag[o] = 0; } void build(int o, int l, int r) { if (l == r) { scanf("%lld", &V[o]); p[o] = l; return; } int mid = (l + r) / 2; build(o << 1, l, mid); build(o << 1 | 1, mid + 1, r); push_up(o); } void upd(int o, int l, int r, int L, int R, long long x) { if (L > R) return; if (L <= l && r <= R) { tag[o] += x; V[o] += x; return; } int mid = (l + r) / 2; push_down(o); if (L <= mid) upd(o << 1, l, mid, L, R, x); if (R > mid) upd(o << 1 | 1, mid + 1, r, L, R, x); push_up(o); } int main() { int n; scanf("%d", &n); build(1, 1, n); for (int i = 1; i <= n; i++) { int pos = p[1]; res[pos] = i; upd(1, 1, n, pos, pos, 1e11); upd(1, 1, n, pos + 1, n, -i); } for (int i = 1; i <= n; i++) { printf("%d ", res[i]); } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
python3
import sys input = sys.stdin.readline n=int(input()) A=list(map(int,input().split())) BIT=[0]*(n+1) def update(v,w): while v<=n: BIT[v]+=w v+=(v&(-v)) def getvalue(v): ANS=0 while v!=0: ANS+=BIT[v] v-=(v&(-v)) return ANS for i in range(1,n+1): update(i,i) ANS=[-1]*n for i in range(n-1,-1,-1): MIN=0 MAX=n k=A[i] while True: x=(MIN+MAX+1)//2 if getvalue(x)>k: if getvalue(x-1)==k: ANS[i]=x break else: MAX=x else: MIN=x update(x,-x) print(*ANS)
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.util.*; import java.lang.*; import java.io.*; public class Main { static final long INF = (long)1e16 + 239; static final int MAXN = (int)1e6 + 239; static long[] a = new long[MAXN]; public static void main (String[] args) throws java.lang.Exception { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver= new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { void solve(int TestCase, InputReader in, PrintWriter out) { int n = in.nextInt(); for(int i = 0; i < n; i++) { a[i] = in.nextLong(); } SegmentTree tree = new SegmentTree(); tree.init(n); int[] ans = new int[n]; for(int i = 0; i < n; i++) ans[i] = -1; for(int i = 1; i <= n; i++) { int pos = tree.last_zero(); ans[pos] = i; tree.add(pos, INF); tree.add(pos + 1, n, -i); } for(int t : ans) { out.print(t + " "); } out.println(); } } static class SegmentTree { int n; long[] t = new long[4 * MAXN]; long[] mod = new long[4 * MAXN]; void pull(int v) { t[v] = Math.min(t[2 * v + 1], t[2 * v + 2]); } void apply(int v, long val) { t[v] += val; mod[v] += val; } void push(int v) { if(mod[v] != 0) { apply(2 * v + 1, mod[v]); apply(2 * v + 2, mod[v]); mod[v] = 0; } } void build(int v, int l, int r) { if(l + 1 == r) { t[v] = a[l]; } else { int m = (r + l) >> 1; build(2 * v + 1, l, m); build(2 * v + 2, m, r); pull(v); } } void add(int v, int l, int r, int ql, int qr, long val) { if(r <= ql || qr <= l) { return; } else if(ql <= l && r <= qr) { apply(v, val); } else { push(v); int m = (r + l) >> 1; add(2 * v + 1, l, m, ql, qr, val); add(2 * v + 2, m, r, ql, qr, val); pull(v); } } int go_down(int v, int l, int r) { if(l + 1 == r) { return l; } else { push(v); int m = (r + l) >> 1; int res = -1; if(t[2 * v + 2] == 0) { res = go_down(2 * v + 2, m, r); } else { res = go_down(2 * v + 1, l, m); } pull(v); return res; } } void init(int _n) { n = _n; build(0, 0, n); } void add(int l, int r, long val) { add(0, 0, n, l, r, val); } void add(int pos, long val) { add(0, 0, n, pos, pos + 1, val); } int last_zero() { return go_down(0, 0, n); } } 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()); } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
python3
class FTree: def __init__(self, f): self.n = len(f) self.ft = [0] * (self.n + 1) for i in range(1, self.n + 1): self.ft[i] += f[i - 1] if i + self.lsone(i) <= self.n: self.ft[i + self.lsone(i)] += self.ft[i] def lsone(self, s): return s & (-s) def query(self, i, j): if i > 1: return self.query(1, j) - self.query(1, i - 1) s = 0 while j > 0: s += self.ft[j] j -= self.lsone(j) return s def update(self, i, v): while i <= self.n: self.ft[i] += v i += self.lsone(i) def select(self, k): lo = 1 hi = self.n for i in range(19): ######## 30 mid = (lo + hi) // 2 if self.query(1, mid) < k: lo = mid else: hi = mid return hi n = int(input()) data = [int(i) for i in input().split()] ft = FTree(list(range(1, n+1))) ans = [""]*n for i in range(n-1, -1, -1): val = data[i] ind = ft.select(val+1) ans[i] = str(ind) ft.update(ind, -ind) print(" ".join(ans))
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int ans[maxn]; int n; long long a[maxn]; const int maxnode = maxn << 2; long long sum[maxnode], add[maxnode]; struct Node { int l, r; int mid() { return (l + r) >> 1; } } node[maxnode]; void Pushup(int rt) { sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; } void Pushdown(int rt, int m) { if (add[rt]) { add[rt << 1] += add[rt]; add[rt << 1 | 1] += add[rt]; sum[rt << 1] += add[rt] * (m - (m >> 1)); sum[rt << 1 | 1] += add[rt] * (m >> 1); add[rt] = 0; } } void build(int l, int r, int rt) { node[rt].l = l; node[rt].r = r; add[rt] = 0; if (l == r) { sum[rt] = (long long)l; return; } int m = node[rt].mid(); build(l, m, rt << 1); build(m + 1, r, rt << 1 | 1); Pushup(rt); } void update(int c, int l, int r, int rt) { if (node[rt].l == l && node[rt].r == r) { add[rt] += c; sum[rt] += (long long)c * (r - l + 1); return; } if (node[rt].l == node[rt].r) return; Pushdown(rt, node[rt].r - node[rt].l + 1); int m = node[rt].mid(); if (r <= m) update(c, l, r, rt << 1); else if (l > m) update(c, l, r, rt << 1 | 1); else { update(c, l, m, rt << 1); update(c, m + 1, r, rt << 1 | 1); } Pushup(rt); } int res; void query(int l, int r, int rt, long long x) { if (l == r) { res = l; return; } int m = node[rt].mid(); Pushdown(rt, m); if (sum[rt << 1] > x && sum[rt << 1] > 0) query(l, m, rt << 1, x); else query(m + 1, r, rt << 1 | 1, x - sum[rt << 1]); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); build(1, n, 1); for (int i = n; i; i--) { query(1, n, 1, a[i]); ans[i] = res; update(-res, res, res, 1); } for (int i = 1; i <= n; i++) printf("%d ", ans[i]); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MAXN = 2e5 + 10; const long long INF = 0x3f3f3f3f; const long long LLINF = 0x3f3f3f3f3f3f3f3f; const long long MOD = 1e9 + 7; long long segtree[MAXN << 2]; long long lazy[MAXN << 2]; long long arr[MAXN]; long long ans[MAXN]; void pushup(long long rt) { segtree[rt] = min(segtree[rt << 1], segtree[rt << 1 | 1]); } void pushdown(long long rt) { if (lazy[rt]) { segtree[rt << 1] = max(0ll, segtree[rt << 1] - lazy[rt]); segtree[rt << 1 | 1] = max(0ll, segtree[rt << 1 | 1] - lazy[rt]); lazy[rt << 1] += lazy[rt]; lazy[rt << 1 | 1] += lazy[rt]; lazy[rt] = 0; } } void build(long long l, long long r, long long rt) { lazy[rt] = 0; if (l == r) { segtree[rt] = arr[l]; return; } long long m = l + r >> 1; build(l, m, rt << 1); build(m + 1, r, rt << 1 | 1); pushup(rt); } long long query(long long l, long long r, long long rt) { if (l == r) { return l; } long long m = l + r >> 1; pushdown(rt); if (!segtree[rt << 1 | 1]) return query(m + 1, r, rt << 1 | 1); else return query(l, m, rt << 1); } void update(long long l, long long r, long long L, long long R, long long C, long long rt) { if (L <= l && r <= R) { segtree[rt] = max(0ll, segtree[rt] - C); lazy[rt] += C; return; } long long m = l + r >> 1; pushdown(rt); if (L <= m) update(l, m, L, R, C, rt << 1); if (R > m) update(m + 1, r, L, R, C, rt << 1 | 1); pushup(rt); } void update(long long l, long long r, long long pos, long long rt) { if (l == r && l == pos) { segtree[rt] = LLINF; return; } long long m = l + r >> 1; pushdown(rt); if (pos <= m) update(l, m, pos, rt << 1); else update(m + 1, r, pos, rt << 1 | 1); pushup(rt); } signed main() { long long n; scanf("%lld", &n); for (long long i = 1; i <= n; ++i) { scanf("%lld", &arr[i]); } build(1, n, 1); long long now = 0; for (long long i = n; i > 0; --i) { long long index = query(1, n, 1); ans[index] = ++now; if (index + 1 <= n) update(1, n, index + 1, n, now, 1); update(1, n, index, 1); } for (long long i = 1; i <= n; ++i) printf("%lld ", ans[i]); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long b[200005]; int n; long long sum(long long x) { if (x == 0) { return 0; } long long pas = 0; while (x > 0) { pas += b[x]; x = x & (x - 1); } return pas; } void update(long long x, long long i) { while (i <= n) { b[i] += x; i += (i & (-i)); } } int main() { cin >> n; long long s[n + 1]; s[0] = 0; int p[n + 1]; p[0] = 0; for (int i = 1; i <= n; i++) { cin >> s[i]; update(i, i); } int mid = 0; for (int i = n; i > 0; i--) { int l = 1; int r = n; while (l != r) { mid = (l + r + 1) / 2; if (sum(mid - 1) <= s[i]) { l = mid; } else { r = mid - 1; } } update(-l, l); p[i] = l; } for (int i = 1; i <= n; i++) { cout << p[i] << " "; } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200010; int n, p[N]; long long a[N], tr[N << 2]; void build(int k, int l, int r) { if (l == r) { tr[k] = l; return; } int mid = l + r >> 1; build(k << 1, l, mid); build(k << 1 | 1, mid + 1, r); tr[k] = tr[k << 1] + tr[k << 1 | 1]; } void add(int k, int l, int r, int v) { if (l == r) { tr[k] = 0; return; } int mid = l + r >> 1; if (v <= mid) add(k << 1, l, mid, v); else add(k << 1 | 1, mid + 1, r, v); tr[k] = tr[k << 1] + tr[k << 1 | 1]; } int query(int k, int l, int r, long long v) { if (l == r) { return l - 1; } int mid = l + r >> 1; if (tr[k << 1] > v) return query(k << 1, l, mid, v); else return query(k << 1 | 1, mid + 1, r, v - tr[k << 1]); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]); build(1, 1, n); for (int i = n; i >= 1; i--) { p[i] = query(1, 1, n, a[i]) + 1; add(1, 1, n, p[i]); } for (int i = 1; i <= n; i++) printf("%d ", p[i]); return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<int, int>; using ld = long double; using pii = pair<int, int>; using dbl = long double; using vll = vector<int>; #pragma GCC optimize("O2") #pragma GCC optimize("unroll-loops") const ll mod = 1000000007; const ll N = 2e5 + 10; const ll inf = 1e16 + 10; ll seg[4 * N], lz[4 * N], a[N], n; ll merge(ll x, ll y) { return min(x, y); } ll cnt(ll l, ll r) { return 1; } void build(ll cur, ll s, ll e) { if (s == e) { seg[cur] = a[s]; return; } build(cur + cur, s, (s + e) / 2); build(cur + cur + 1, (s + e) / 2 + 1, e); seg[cur] = merge(seg[cur + cur], seg[cur + cur + 1]); } void propogate(ll cur, ll s, ll e) { if (s != e) { lz[cur + cur] += lz[cur]; lz[cur + cur + 1] += lz[cur]; } seg[cur] += cnt(s, e) * lz[cur]; lz[cur] = 0; } void update(ll cur, ll s, ll e, ll l, ll r, ll val) { propogate(cur, s, e); if (e < l || s > r) return; if (l <= s && e <= r) { lz[cur] += val; propogate(cur, s, e); return; } update(cur + cur, s, (s + e) / 2, l, r, val); update(cur + cur + 1, (s + e) / 2 + 1, e, l, r, val); seg[cur] = merge(seg[cur + cur], seg[cur + cur + 1]); } ll query(ll cur, ll s, ll e, ll l, ll r) { propogate(cur, s, e); if (e < l || s > r) return inf; if (l <= s && e <= r) return seg[cur]; ll q1 = query(cur + cur, s, (s + e) / 2, l, r); ll q2 = query(cur + cur + 1, (s + e) / 2 + 1, e, l, r); return merge(q1, q2); } ll dfs(ll cur, ll s, ll e) { propogate(cur, s, e); if (s == e) { return s; } propogate(cur + cur, s, (s + e) / 2); propogate(cur + cur + 1, (s + e) / 2 + 1, e); if (seg[cur + cur + 1] == 0) { return dfs(cur + cur + 1, (s + e) / 2 + 1, e); } else { return dfs(cur + cur, s, (s + e) / 2); } } void build() { build(1, 1, n); } void update(ll pos, ll val) { update(1, 1, n, pos, pos, val); } void update(ll l, ll r, ll val) { update(1, 1, n, l, r, val); } ll query(ll l, ll r) { return query(1, 1, n, l, r); } signed main() { cin >> n; for (ll i = 1; i <= n; i++) { cin >> a[i]; } build(); ll ans[n]; for (ll i = 0; i < n; i++) { ll x = dfs(1, 1, n); update(x, inf); update(x + 1, n, -(i + 1)); ans[x - 1] = i + 1; } for (ll i = 0; i < n; i++) { cout << ans[i] << " "; } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.PrintWriter; public class D1208 { static class Scanner { BufferedReader br; StringTokenizer tk = new StringTokenizer(""); public Scanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } public int nextInt() throws IOException { if (tk.hasMoreTokens()) return Integer.parseInt(tk.nextToken()); tk = new StringTokenizer(br.readLine()); return nextInt(); } public long nextLong() throws IOException { if (tk.hasMoreTokens()) return Long.parseLong(tk.nextToken()); tk = new StringTokenizer(br.readLine()); return nextLong(); } public String next() throws IOException { if (tk.hasMoreTokens()) return (tk.nextToken()); tk = new StringTokenizer(br.readLine()); return next(); } public String nextLine() throws IOException { tk = new StringTokenizer(""); return br.readLine(); } public double nextDouble() throws IOException { if (tk.hasMoreTokens()) return Double.parseDouble(tk.nextToken()); tk = new StringTokenizer(br.readLine()); return nextDouble(); } public char nextChar() throws IOException { if (tk.hasMoreTokens()) return (tk.nextToken().charAt(0)); tk = new StringTokenizer(br.readLine()); return nextChar(); } 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 int[] nextIntArrayOneBased(int n) throws IOException { int a[] = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextInt(); return a; } public long[] nextLongArrayOneBased(int n) throws IOException { long a[] = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextLong(); return a; } } public static void main(String args[]) throws IOException { new Thread(null, new Runnable() { public void run() { try { solve(); } catch (Exception e) { e.printStackTrace(); } } }, "1", 1 << 26).start(); } static class SegmentTree { int n; long tree[]; int start[]; int end[]; long lazy[]; public SegmentTree(long a[]) { n=a.length+1; tree=new long[4*n+1]; start=new int[4*n+1]; end=new int[4*n+1]; lazy=new long[4*n+1]; initialize(1,0,n-2,a); } void initialize(int node,int s,int e,long a[]) { start[node]=s; end[node]=e; if(s==e) { tree[node]=a[s]; return; } int mid=(s+e)/2; initialize(node<<1,s,mid,a); initialize((node<<1)|1,mid+1,e,a); tree[node]=Math.min(tree[node<<1], tree[(node<<1)|1]); } void lazyPropagation(int node) { lazy[node<<1]+=lazy[node]; lazy[(node<<1)|1]+=lazy[node]; lazy[node]=0; } void update(int node) { long leftValue=tree[node<<1]+lazy[node<<1]; long rightValue=tree[(node<<1)|1]+lazy[(node<<1)|1]; tree[node]=Math.min(leftValue,rightValue); } void incrementRange(int l,int r,long value) { incrementRange(1,l,r,value); } void incrementRange(int node,int l,int r,long value) { if(start[node]>r||end[node]<l) return; if(start[node]>=l&&end[node]<=r) { lazy[node]+=value; return; } lazyPropagation(node); incrementRange(node<<1,l,r,value); incrementRange((node<<1)|1,l,r,value); update(node); } int query() { return query(1); } int query(int node) { if(tree[node]+lazy[node]!=0) return -1; if(start[node]==end[node]) return start[node]; lazyPropagation(node); update(node); int rightValue=query((node<<1)|1); if(rightValue!=-1) return rightValue; return query(node<<1); } } static void solve() throws IOException { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n=in.nextInt(); long p[]=in.nextLongArray(n); SegmentTree st=new SegmentTree(p); int a[]=new int[n]; for(int i=1;i<=n;i++){ int index=st.query(); a[index]=i; if(index!=n-1){ st.incrementRange(index+1, n-1, -i); } st.incrementRange(index,index,1000000000000l); } for(int i=0;i<n;i++){ out.print(a[i]+" "); } out.println(); out.close(); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long int fen[200005]; void upd(long long int x, long long int a) { for (long long int i = x; i < 200005; i += (i & (-i))) { fen[i] += a; } } long long int ret(long long int x) { long long int o = 0; for (long long int i = x; i > 0; i -= (i & (-i))) { o += fen[i]; } return o; } int main() { long long int n; cin >> n; long long int s[n]; for (long long int i = 0; i < n; i++) { cin >> s[i]; upd(i + 1, i + 1); } long long int out[n]; for (long long int i = n - 1; i >= 0; i--) { long long int l = 0, h = n - 1; while (h - l > 1) { long long int mi = (h + l) / 2; if (ret(mi) > s[i]) { h = mi; } else { l = mi; } } if (ret(h) == s[i]) { out[i] = h + 1; upd(h + 1, -h - 1); } else { out[i] = l + 1; upd(l + 1, -l - 1); } cout << "\n"; } for (long long int i = 0; i < n; i++) { cout << out[i] << " "; } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
java
import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.net.Inet4Address; import java.util.*; import java.lang.*; import java.util.HashMap; import java.util.PriorityQueue; public class Solution implements Runnable{ static class pair implements Comparable { long f; int s; pair(long fi,int se) { f=fi; s=se; } public int compareTo(Object o)//ascending order { pair pr=(pair)o; if(s>pr.s) return 1; if(s==pr.s) { if(f>pr.f) return 1; else return -1; } else return -1; } public boolean equals(Object o) { pair ob=(pair)o; long ff; int ss; if(o!=null) { ff=ob.f; ss=ob.s; if((ff==this.f)&&(ss==this.s)) return true; } return false; } public int hashCode() { return (this.f+" "+this.s).hashCode(); } } public class triplet implements Comparable { int f,t; int s; triplet(int f,int s,int t) { this.f=f; this.s=s; this.t=t; } public boolean equals(Object o) { triplet ob=(triplet)o; int ff; int ss; int tt; if(o!=null) { ff=ob.f; ss=ob.s; tt=ob.t; if((ff==this.f)&&(ss==this.s)&&(tt==this.t)) return true; } return false; } public int hashCode() { return (this.f+" "+this.s+" "+this.t).hashCode(); } public int compareTo(Object o)//ascending order { triplet tr=(triplet)o; if(t>tr.t) return 1; else return -1; } } void merge1(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[] = new int [n1]; int R[] = new int [n2]; for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i]<=R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } void sort1(int arr[], int l, int r) { if (l < r) { int m = (l+r)/2; sort1(arr, l, m); sort1(arr , m+1, r); merge1(arr, l, m, r); } } long a[]; pair tree[]; long lazy[]; long inf=(long)100000000*100000000; void build(int node,int start,int end) { if(start==end) { tree[node].f=a[start]; tree[node].s=start; return; } int mid=(start+end)/2; build(2*node,start,mid); build(2*node+1,mid+1,end); if(tree[2*node].f<tree[2*node+1].f) tree[node]=new pair(tree[2*node].f,tree[2*node].s); else if(tree[2*node+1].f<tree[2*node].f) tree[node]=new pair(tree[2*node+1].f,tree[2*node+1].s); else tree[node]=new pair(tree[2*node+1].f,tree[2*node+1].s); } pair query(int node,int start,int end) { if(lazy[node]!=0) { tree[node].f+=lazy[node]; if(start!=end) { lazy[2*node]+=lazy[node]; lazy[2*node+1]+=lazy[node]; } lazy[node]=0; } return tree[node]; } void update(int node,int start,int end,int l,int r,long val) { if(lazy[node]!=0) { tree[node].f+=lazy[node]; if(start!=end) { lazy[2*node]+=lazy[node]; lazy[2*node+1]+=lazy[node]; } lazy[node]=0; } if(end<l || r<start) return; if(start>=l && end<=r) { tree[node].f+=val; if(start!=end) { lazy[2*node]+=val; lazy[2*node+1]+=val; } return; } int mid=(start+end)/2; update(2*node,start,mid,l,r,val); update(2*node+1,mid+1,end,l,r,val); if(tree[2*node].f<tree[2*node+1].f) tree[node]=new pair(tree[2*node].f,tree[2*node].s); else if(tree[2*node+1].f<tree[2*node].f) tree[node]=new pair(tree[2*node+1].f,tree[2*node+1].s); else tree[node]=new pair(tree[2*node+1].f,tree[2*node+1].s); } public static void main(String args[])throws Exception { new Thread(null,new Solution(),"Solution",1<<27).start(); } public void run() { try { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int n=in.ni(); a=new long[n+1]; tree=new pair[4*n+1]; lazy=new long[4*n+1]; for(int i=1;i<=4*n;i++) tree[i]=new pair(0,0); for(int i=1;i<=n;i++) a[i]=in.nl(); build(1,1,n); /*for(int i=1;i<=5;i++) System.out.println(tree[i].f+" "+tree[i].s); System.out.println();*/ int ans[]=new int[n+1]; int k=1; while(k<=n) { pair p=query(1,1,n); //System.out.println(p.s); ans[p.s]=k; update(1,1,n,p.s,p.s,inf); update(1,1,n,p.s+1,n,-1*k); k++; /*for(int i=1;i<=5;i++) System.out.println(tree[i].f+" "+tree[i].s); System.out.println();*/ } for(int i=1;i<=n;i++) out.print(ans[i]+" "); out.close(); } catch(Exception e){ return; } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream stream) { this.stream = stream; } 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 ni() { 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 nl() { 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] = ni(); } 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; } } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MAXN = 200100; const long long INF = 2000000100; const long long MOD = 998244353; long long N, S[MAXN], P[MAXN]; long long B[MAXN]; void update(int ind, long long v) { for (int i = ind; i <= N; i += (i & -i)) { B[i] += v; } } long long query(int ind) { if (ind == 0) return 0; long long s = 0; for (int i = ind; i > 0; i -= (i & -i)) { s += B[i]; } return s; } int main() { ios_base::sync_with_stdio(0); cin >> N; for (int i = 0; i < N; i++) cin >> S[i]; for (int i = 1; i <= N; i++) update(i, i); for (int i = N - 1; i >= 0; i--) { long long a = 1, b = N, c; long long ans = -39; while (a <= b) { c = (a + b) / 2; if (query(c - 1) > S[i]) { b = c - 1; } else { ans = c; a = c + 1; } } P[i] = ans; update(ans, -ans); } for (int i = 0; i < N; i++) cout << P[i] << (i == N - 1 ? "\n" : " "); }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
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 int MAXN = 5000; private static final String NO = "NO"; private static final String YES = "YES"; InputStream is; PrintWriter out; String INPUT = ""; private static long MOD; private static final int MAX = Integer.MAX_VALUE / 4; long BIT[]; void update(int ind, long vl) { for (int i = ind; i < BIT.length; i += (i & -i)) BIT[i] += vl; } long query(int ind) { long ans = 0; for (int i = ind; i >= 1; i -= (i & -i)) { ans += BIT[i]; } return ans; } int search(long prefSum) { int num = 0; long sum = 0; for (int i = 21; i >= 0; --i) { if ((num + (1 << i) < BIT.length) && (sum + BIT[num + (1 << i)] <= prefSum)) { num += (1 << i); sum += BIT[num]; } } return num + 1; } void solve() { int N = ni(); BIT = new long[N + 1]; long pre[] = new long[N]; for (int i = 1; i <= N; i++) { update(i, i); pre[i - 1] = nl(); } for (int i = N - 1; i >= 0; i--) { pre[i] = search(pre[i]); update((int)pre[i], -pre[i]); } for (long i : pre) out.print(i + " "); } 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 boolean vis[]; 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 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 Integer[][] na2(int n, int m) { Integer[][] a = new Integer[n][]; for (int i = 0; i < n; i++) a[i] = na2(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[] nl(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); 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)); } }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct node { int64_t data, lazy; int pos; }; node st[200001 * 4]; int64_t input[200001]; int anw[200001]; void output(int root, int L, int R) { if (L == R) { return; } int mid = (L + R) >> 1; output(root << 1, L, mid); output(root << 1 | 1, mid + 1, R); } void Init(int root, int L, int R) { st[root].lazy = 0; if (L == R) { st[root].data = input[L]; st[root].pos = L; return; } int mid = (L + R) >> 1; Init(root << 1, L, mid); Init(root << 1 | 1, mid + 1, R); int lpos = root << 1, rpos = root << 1 | 1; st[root].data = min(st[lpos].data, st[rpos].data); if (st[root].data == st[rpos].data) { st[root].pos = st[rpos].pos; } else { st[root].pos = st[lpos].pos; } } void Update(int root, int l, int r, int L, int R, int64_t val) { if (l == L && r == R) { st[root].lazy += val; st[root].data += val; return; } int mid = (L + R) >> 1; if (st[root].lazy != 0) { Update(root << 1, L, mid, L, mid, st[root].lazy); Update(root << 1 | 1, mid + 1, R, mid + 1, R, st[root].lazy); st[root].lazy = 0; } if (r <= mid) { Update(root << 1, l, r, L, mid, val); } else if (mid < l) { Update(root << 1 | 1, l, r, mid + 1, R, val); } else { Update(root << 1, l, mid, L, mid, val); Update(root << 1 | 1, mid + 1, r, mid + 1, R, val); } int lpos = root << 1, rpos = root << 1 | 1; st[root].data = min(st[lpos].data, st[rpos].data); if (st[root].data == st[rpos].data) { st[root].pos = st[rpos].pos; } else { st[root].pos = st[lpos].pos; } } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> input[i]; } Init(1, 1, n); int64_t MAX = int64_t(n) * int64_t(n) * int64_t(100); if (n == 2) { output(1, 1, n); } for (int i = 1; i <= n; i++) { int pos = st[1].pos; anw[pos] = i; Update(1, pos, pos, 1, n, MAX); Update(1, pos, n, 1, n, int64_t(i * -1)); if (n == 2) { output(1, 1, n); } } for (int i = 1; i <= n; i++) { cout << anw[i] << " "; } return 0; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 10, Max = 101; const int MOD = 1000000007; const long long OO = 2e12 + 5000; const long long Sqrt = 710; const double PI = acos(-1); const double EPS = 1e-9; int n; long long a[N]; pair<long long, long long> seg[4 * N]; long long lazy[8 * N]; void push(int p) { if (!lazy[p]) return; seg[p].first += lazy[p]; lazy[p * 2] += lazy[p]; lazy[p * 2 + 1] += lazy[p]; lazy[p] = 0; } void build(int p = 1, int s = 1, int e = n) { if (s == e) { seg[p] = {a[s], -s}; return; } int mid = (s + e) / 2; build(p * 2, s, mid); build(p * 2 + 1, mid + 1, e); seg[p] = min(seg[p * 2], seg[p * 2 + 1]); } void update(int from, int to, long long val, int p = 1, int s = 1, int e = n) { push(p); if (s > to || from > e) return; if (s >= from && e <= to) { lazy[p] += val; push(p); return; } int mid = (s + e) / 2; update(from, to, val, p * 2, s, mid); update(from, to, val, p * 2 + 1, mid + 1, e); seg[p] = min(seg[p * 2], seg[p * 2 + 1]); } pair<long long, long long> get(int from = 1, int to = n, int p = 1, int s = 1, int e = n) { push(p); if (s > to || from > e) return {OO, OO}; if (s >= from && e <= to) { return seg[p]; } int mid = (s + e) / 2; pair<long long, long long> first = get(from, to, p * 2, s, mid); pair<long long, long long> second = get(from, to, p * 2 + 1, mid + 1, e); return min(first, second); } int main() { cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; } vector<int> sol(N); build(); int cnt = 0; for (int i = 1; i <= n; ++i) { pair<long long, long long> mn = get(); int idx = mn.second * -1; assert(mn.second != OO); sol[idx] = i; long long val = OO; update(idx, idx, val); val = i; update(1 + idx, n, -val); } for (int i = 1; i <= n; ++i) cout << sol[i] << " "; cout << endl; }
1208_D. Restore Permutation
An array of integers p_{1},p_{2}, …,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4]. There is a hidden permutation of length n. For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element. Your task is to restore the permutation. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^{5}) β€” the size of the permutation. The second line contains n integers s_{1}, s_{2}, …, s_{n} (0 ≀ s_{i} ≀ (n(n-1))/(2)). It is guaranteed that the array s corresponds to a valid permutation of length n. Output Print n integers p_{1}, p_{2}, …, p_{n} β€” the elements of the restored permutation. We can show that the answer is always unique. Examples Input 3 0 0 0 Output 3 2 1 Input 2 0 1 Output 1 2 Input 5 0 1 1 1 10 Output 1 4 3 2 5 Note In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0. In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1. In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
{ "input": [ "3\n0 0 0\n", "5\n0 1 1 1 10\n", "2\n0 1\n" ], "output": [ "3 2 1 ", "1 4 3 2 5 ", "1 2 " ] }
{ "input": [ "100\n0 0 57 121 57 0 19 251 19 301 19 160 57 578 664 57 19 50 0 621 91 5 263 34 5 96 713 649 22 22 22 5 108 198 1412 1147 84 1326 1777 0 1780 132 2000 479 1314 525 68 690 1689 1431 1288 54 1514 1593 1037 1655 807 465 1674 1747 1982 423 837 139 1249 1997 1635 1309 661 334 3307 2691 21 3 533 1697 250 3920 0 343 96 242 2359 3877 3877 150 1226 96 358 829 228 2618 27 2854 119 1883 710 0 4248 435\n", "20\n0 1 7 15 30 15 59 42 1 4 1 36 116 36 16 136 10 36 46 36\n", "1\n0\n", "15\n0 0 3 3 13 3 6 34 47 12 20 6 6 21 55\n" ], "output": [ "94 57 64 90 58 19 53 71 50 67 38 56 45 86 89 42 31 36 5 68 37 10 49 24 7 32 65 59 14 12 11 6 27 34 91 72 21 87 98 3 97 25 100 46 85 48 18 51 88 83 70 13 79 82 62 80 55 43 73 76 81 40 52 22 60 77 69 61 47 35 92 84 9 4 41 66 28 99 2 33 17 26 74 96 95 20 54 15 29 44 23 75 8 78 16 63 39 1 93 30 ", "1 6 8 15 17 12 18 16 3 4 2 14 20 13 7 19 5 10 11 9 ", "1 ", "2 1 15 10 12 3 6 13 14 8 9 5 4 7 11 " ] }
CORRECT
python3
# https://codeforces.com/contest/1208/problem/D from sys import stdin, stdout input = stdin.readline # print = stdout.write # si is the sum of elements before the i-th element that are smaller than the i-th element. # For every i from N to 1, let's say the value of the si is x. # So it means there are k smallest unused numbers whose sum is x. # We simply put the k+1st number in the output permutation at this i, and continue to move left. # BIT and binary lifting # https://codeforces.com/contest/1208/submission/59526098 class BIT: def __init__(self, nums): # we store the sum information in bit 1. # so the indices should be 1 based. # here we assume nums[0] = 0 self.nums = [0] * (len(nums)) for i, x in enumerate(nums): if i == 0: continue self.update(i, x) def low_bit(self, x): return x & (-x) def update(self, i, diff): while i < len(self.nums): self.nums[i] += diff i += self.low_bit(i) def prefix_sum(self, i): ret = 0 while i != 0: ret += self.nums[i] i -= self.low_bit(i) return ret def search(self, x): # find the index i such that prefix_sum(i) == x cur_index, cur_sum = 0, 0 delta = len(self.nums) - 1 while delta - self.low_bit(delta): delta -= self.low_bit(delta) while delta: m = cur_index + delta if m < len(self.nums): sm = cur_sum + self.nums[m] if sm <= x: cur_index, cur_sum = m, sm delta //= 2 return cur_index + 1 n = int(input()) bit = BIT(range(n+1)) ans = [0 for _ in range(n)] nums = list(map(int, input().split())) for i in range(n - 1, -1, -1): index = bit.search(nums[i]) bit.update(index, -index) ans[i] = index print(*ans)