prompt
string
response
string
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
n,k=map(int,input().split()) *a,=map(int,input().split()) g=list(range(n+1))[1:] n-=1 v=0 for i in a: v+=i v%=len(g) print(g.pop(v),end=' ')
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
import java.util.Scanner; import java.util.Vector; public class CF_792B { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = in.nextInt(); Vector<Integer> children = new Vector<Integer>(); for (int i = 0; i < n; i++) { children.add(i + 1); } ...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#include <bits/stdc++.h> using namespace std; int n, k, j, lan, dau; int A[101]; int main() { cin >> n >> k; dau = n; memset(A, 1, sizeof(A)); j = 1; for (int i = 1; i <= k; i++) { cin >> lan; if (lan % n == 0) lan = n; else lan %= n; for (int i = 1; i <= lan; i++) { j++; ...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
n,y=input().split() n=int(n) y=int(y) liste=list(range(1,n+1)) x=0 z=0 liste2=list(map(int,input().split())) liste3=[] while x<y: z=((z+liste2[x])%len(liste)) liste3.append(liste[z]) liste.pop(z) x+=1 for i in liste3: print(i)
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#include <bits/stdc++.h> using namespace std; list<int> a; int main() { int n, k, m; scanf("%d %d", &n, &k); for (int i = 1; i <= n; i++) { a.push_back(i); } list<int>::iterator it = a.begin(), tmp; while (k--) { scanf("%d", &m); m %= a.size(); for (int i = 0; i < m; i++) { it++; ...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; ...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#include <bits/stdc++.h> using namespace std; long long modpow(long long base, long long exp, long long modulus) { base %= modulus; long long result = 1; while (exp > 0) { if (exp & 1) result = (result * base) % modulus; base = (base * base) % modulus; exp >>= 1; } return result; } long long find(...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
import sys I=sys.stdin.readline n,k=map(int,input().split()) arr=list(map(int,input().split())) c=list(range(n)) ans=[] s=0 for i in arr: s=(s+i)%len(c) ans.append(1+c.pop(s)) print(*ans)
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a; for (int i = 0; i < n; i++) a.push_back(i + 1); int leader = 0; for (int i = 0; i < k; i++) { int r; int cnt = 0; cin >> r; int elim = (r + leader) % a.size(); cout << a[elim] << " "; a...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
//package Test; import java.util.*; /** * Created by denis on 04.04.17. */ public class codeforces { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(), k = input.nextInt(); Integer mas_k[] = new Integer[k]; // вводим масси...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
import java.io.*; import java.util.ArrayList; import java.util.StringTokenizer; // Hemouz public class Main { static PrintWriter out; static InputReader in; static InputStream inputs; public static void main(String[] args) { inputs = System.in; in = new InputReader(inputs); ou...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { public static InputReader in; public static PrintWriter pw; public static void main(String args[]) { new Thread(null, new Runnable() { pub...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#!/usr/bin/env python n,k=map(int,raw_input().split()) a=map(int,raw_input().split()) #ix=[(i,x) for i,x in enumerate(range(n))] member=range(n) leader_index=0 for i in range(k): # print str(i)+"th step" # print 'leader: '+str(leader_index)+' a[i]: '+str(a[i]) end=(leader_index+1+a[i])%n-1 ###end:index # ...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#include <bits/stdc++.h> using namespace std; const int N_MAX = 100 + 5; int n, k, a; bitset<N_MAX> in_game; vector<int> v; int main() { cin >> n >> k; v.resize(n); for (int i = 0; i < n; ++i) v[i] = i + 1; int leader = 0; for (int test = 0; test < k; ++test) { cin >> a; leader = (leader + a) % n; ...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
import java.io.*; import java.util.*; public class TestClass { static PrintWriter out = new PrintWriter(System.out); public static void main(String args[]) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s[] = in.readLine().split(" "); int n = Integer.parse...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#include <bits/stdc++.h> using namespace std; int n, k, x; vector<int> a; int pos = 1; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) { a.push_back(i); } for (int i = 1; i <= k; i++) { cin >> x; x %= a.size(); if (pos + x > a.size()) { x -= (a.size() - pos); } else { x ...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#include <bits/stdc++.h> using namespace std; int main() { int no_of_children, no_of_steps, step_size; cin >> no_of_children >> no_of_steps; vector<int> children; for (int i(0); i < (no_of_children); i++) { children.push_back(i + 1); } int current_index_of_child = 0, children_left = no_of_children; fo...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
n,k = map(int,raw_input().split()) a = map(int,raw_input().split()) inp = [] ans=[] for i in xrange(n): inp.append(i+1) start=0 for i in xrange(k): j = (start+a[i])%n start=j%n ans.append(inp[j]) inp.remove(inp[j]) n-=1 for i in ans: print i,
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
N, K = map(int, raw_input().split()) A = map(int, raw_input().split()) NI = [i for i in range(1, N+1)] result = [] leader = 0 for Ai in A: rem = Ai % N remove = (leader+rem) % N leader = remove % (N-1) N -= 1 result.append(NI[remove]) NI.pop(remove) print " ".join(map(str, result))
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#include <bits/stdc++.h> using namespace std; int n, k, a[200]; queue<int> q; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) q.push(i); for (int i = 1; i <= k; i++) cin >> a[i]; for (int i = 1; i <= k; i++) { a[i] %= q.size(); for (int j = 1; j <= a[i]; j++) { q.push(q.front()); q....
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
e=1 n,k=map(int,input().split()) n1=list(range(n+1)[1:n+1]) a=list(map(int,input().split())) for i in range(k): j=((e+a[i])%n)-1 if (e+a[i])%n==0: j=n-1 e=1 else: e=j+1 print(n1.pop(j)) n-=1
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#include <bits/stdc++.h> using namespace std; int pl[102], K[102]; int main() { int n, k; cin >> n >> k; for (int i = 0; i < n; i++) pl[i] = i + 1; for (int i = 0; i < k; i++) scanf("%d", &K[i]); int leader = 0; for (int i = 0; i < k; i++) { int rem = K[i] % n; int elm = (rem + leader) % n; prin...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#include <bits/stdc++.h> using namespace std; int main() { long long n, k, l = 0; cin >> n >> k; bool vis[n + 1]; memset(vis, true, sizeof(vis)); vector<long long> a; for (long long i = 0; i < n; i++) a.push_back(i + 1); for (int i = 0; i < k; i++) { long long s, j = l; cin >> s; s = s % a.siz...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class B { public B() { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); int k = sc.nextInt(); boolean[] child = new boolean[n + 1]; Arrays.fill(child, true); in...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL); ; long n, k; cin >> n >> k; vector<int> ar(n); for (int i = 0; i < n; i++) cin >> ar[i]; queue<int> q; for (int i = 1; i <= n; i++) q.push(i); int i = 0; while (!q.empty() && (k--)) { int ...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
n, k = map(int, raw_input().split()) a = range(1, n + 1) cnts = map(int, raw_input().split()) leader = 0 res = [] for cnt in cnts: cnt %= len(a) i = (leader + cnt) % len(a) res.append(str(a[i])) del a[i] leader = i print ' '.join(res) ''' 7 5 10 4 11 4 1 3 2 2 5 '''
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#include <bits/stdc++.h> using namespace std; const size_t MAX = 200000 + 1; uint64_t gcd(uint64_t a, uint64_t b) { return b ? gcd(b, a % b) : a; } int main(int argc, char *argv[]) { ios_base::sync_with_stdio(false); ifstream in("input.txt"); ofstream out("output.txt"); int32_t n, k, a, v = 0; cin >> n >> k; ...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#include <bits/stdc++.h> using namespace std; bool eliminated[101]; int next(int a, int n) { a = (a + 1) % n; while (true) { if (!eliminated[a]) return a; a = (a + 1) % n; } } int main() { int n, k; scanf("%d %d", &n, &k); int a[k + 1]; for (int i = 0; i < k; i++) { scanf("%d", &a[i]); } i...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#include <bits/stdc++.h> using namespace std; template <class T, class Compare = less<T>, class Alloc = allocator<T> > class set; template <class T, class Alloc = allocator<T> > class vector; template <class Key, class T, class Compare = less<Key>, class Alloc = allocator<pair<const Key, T> > > class map; tem...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
N, K = map(int, input().split()) As = map(int, input().split()) cs = [c for c in range(1, N + 1)] ans = [] for a in As: i = a % len(cs) ans.append(cs[i]) cs = cs[i + 1:] + cs[:i] print(' '.join(map(str, ans)))
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
s = input().split() n = int(s[0]) k = int(s[1]) s = input().split() v = [] for w in s: v.append(int(w)) kids = [] for i in range(1, n + 1): kids.append(i) actual = 0 for i in range(k): actual = (actual + v[i]) % n print(kids.pop(actual)) n -= 1 actual = actual % n
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#include <bits/stdc++.h> using namespace std; int a[110]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) cin >> a[i]; vector<int> v; for (int i = 1; i <= n; i++) v.push_back(i); int lea = 0; for (int i = 0; i < m; i++) { lea = (lea + a[i]) % v.size(); cout << v[lea] << " "; ...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#include <bits/stdc++.h> using namespace std; int main() { int n, k; scanf("%d %d", &n, &k); int a[k + 1]; for (int i = 1; i <= k; i++) scanf("%d", &a[i]); int st = 0; vector<int> v; for (int i = 0; i < n; i++) v.push_back(i); int cur, temp; int sz = n; for (int i = 1; i <= k; i++) { temp = a[i]...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const int inf = 0x3f3f3f3f; const double eps = 1e-7; const int maxn = 105; int n, k, a[maxn], vis[maxn]; int main() { ios::sync_with_stdio(0), cin.tie(0); memset(vis, 0, sizeof(vis)); cin >> n >> k; int now = 1, cnt = n; for (int ...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
//package codeForces.contest_EducationalCodeforcesRound18; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.Deque; import java.util.LinkedList; public class p02_countingOutRhyme { public static void main(String[] args) thro...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
# coding=utf-8 s = raw_input().split(' ') n = int(s[0]) k = int(s[1]) s = raw_input().split(' ') child = [] result = [] for i in range(1, n + 1): child.append(i) cur = 0 for i in range(0, k): cur = (cur + int(s[i])) % len(child) result.append(str(child.pop(cur))) print ' '.join(result) ...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
##n = int(input()) ##a = list(map(int, input().split())) ##print(' '.join(map(str, res))) [n, k] = list(map(int, input().split())) a = list(map(int, input().split())) p = [i+1 for i in range(n)] leader = 0 res = [] for i in range(k): leader = (leader+a[i])%n res.append(p.pop(leader)) n = len(p) if lea...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#include <bits/stdc++.h> using namespace std; const int MAXN = 110; int pv[MAXN], nx[MAXN]; void del(int x) { nx[pv[x]] = nx[x]; pv[nx[x]] = pv[x]; } int main() { int n, k; scanf("%d%d", &n, &k); int left = n; for (int i = 1; i <= n; i++) { pv[i] = i - 1; nx[i] = i + 1; } pv[1] = n; nx[n] = 1;...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
def solution(n, nums): child = [x for x in range(1,n+1)] #result = [] lead = 0 for a in nums: steps = a%len(child) borr = (lead + steps) % len(child) #result.append(children[next_index]) print(child[borr] , end = " ") del child[borr] lead = borr def main...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int a[k]; for (int i = 0; i < k; i++) cin >> a[i]; list<int> leader; for (int i = 1; i <= n; i++) leader.insert(leader.end(), i); int l = 1; int i = 0; int begin = 0; while (k--) { l = (leader.size() + a[i] + beg...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
__author__ = 'Alexander' n, k = map(int, input().split()) a = [int(x) for x in input().split()] loose = [] first = 0 for i in range(k): loose.append(((a[i] + first) % (n-i))+1) first = (a[i] + first) % (n-i) lst = list(range(1, n+1)) res = '' for i in range(k): res += str(lst[loose[i]-1]) + ' ' lst.rem...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
def solution(n, nums): lis = [x for x in range(n)] pivot = 0 for a in nums: steps = a%len(lis) borr = (pivot + steps) % len(lis) print(lis[borr] +1 , end = " ") del lis[borr] pivot = borr def main(): n, k = [int(x) for x in input().split()] a = [int(x) for x...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#include <bits/stdc++.h> using namespace std; int a[110]; int ans[110]; bool use[110]; int main() { int n, k; cin >> n >> k; for (int i = 0; i < k; i++) { cin >> a[i]; } int cur = 0; for (int i = 0; i < k; i++) { int inc = a[i] % (n - i); for (int j = 0; j < inc; j++) { cur++; if (cu...
Problem: n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The ...
#include <bits/stdc++.h> using namespace std; int a[110]; int main() { int n, k; cin >> n >> k; vector<int> v; for (int i = 0; i < k; i++) cin >> a[i]; for (int i = 1; i <= n; i++) v.push_back(i); int lea = 0; for (int i = 0; i < k && v.size() > 0; i++) { lea %= v.size(); int next = (lea + a[i]) %...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> #pragma warning(disable : 4996) #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("-ffloat-store") using namespace std; clock_t time_p = clock(); void aryanc403() { time_...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; class TaskD { private: vector<int> tin, tout, sz; int t = 1; void dfs(int u, const vector<vector<int>>& g) { tin[u] = t++; sz[u] = 1; for (int v : g[u]) { dfs(v, g); sz[u] += sz[v]; } tout[u] = t++; } bool in_subtree(int u, int v) ...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 1e5 + 5; int n, m, k, q; vector<int> g[maxn]; int in[maxn], out[maxn]; int vis[maxn]; int dfs_clock; void dfs(int u) { in[u] = ++dfs_clock; for (int i = 0; i < g[u].size(); i++) { dfs(g[u][i]); } out[u] = dfs_clock; }...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } int child, toys, scenes, queries; vector<int> vec[123456]; int parent[123456], custodyBy[123456], subtree[123456], vis[123456], roox[123456], tin[123456], tout[123456]; int tim =...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; int n, m, k, q, subtree[100005], last_play[100005], a, b, c, tin[100005], tout[100005], time_in = 1, time_out = 1, x, y, z, par[100005]; vector<int> g[100005]; bool used[100005], use[100005]; int dfs_get(int v) { used[v] = true; tin[v] = time_in; time_in++; int ...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; inline int read() { int s = 0, t = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') t = -1; c = getchar(); } while (isdigit(c)) s = s * 10 + c - 48, c = getchar(); return s * t; } const int N = 1e6 + 7; vector<int> S[N]; int L[N], R[N], g[N], s...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
// Code Author: Rutul Patel import java.io.*; import java.math.BigInteger; import java.math.RoundingMode; import java.text.DecimalFormat; import java.util.*; public class Main { static double eps=(double)1e-6; static long mod=(int)1e9+6; static ArrayList<Integer>[] list; static int[] size; static boo...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; vector<vector<int> > v; int k = 0, st[100009] = {}, idx[100009] = {}; void dfs(int i) { idx[i] = k; k++; int d, j, s = 0; for (j = 0; j < v[i].size(); j++) { d = v[i][j]; dfs(d); s = s + st[d]; } st[i] = s + 1; return; } int query(int a, int b) { ...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; const int INF = 1001001001; const long long INFL = 1001001001001001001LL; template <typename T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; } template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template ...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; int n, m, k, q; vector<int> G[MAXN]; int pre[MAXN]; int belong[MAXN]; int sz[MAXN]; int id[MAXN]; int num = 1; int dfsclock = 0; int in[MAXN], out[MAXN]; void dfs(int u, int fa) { sz[u] = 1; id[u] = num; in[u] = ++dfsclock; int ee = G[u].s...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const double eps = 1e-8; int p[100005]; int a[100005], b[100005]; vector<int> edges[100005]; vector<int> rev[100005]; int last[100005]; bool inc[100005]; bool visited[100005]; queue<int> qu; int cnt, num; int dfs_num[100005]; int dfs_low[100005];...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; int n, m, k, q, dep[100005], tin[100005], tout[100005], sz[100005], t; vector<int> graph[100005]; void go(int now) { tin[now] = ++t; sz[now] = 1; for (int i = 0; i < graph[now].size(); i++) { go(graph[now][i]); sz[now] += sz[graph[now][i]]; } tout[now] = +...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; vector<int> tree[100000 + 2]; int sz[100000 + 2]; int tin[100000 + 2]; int tout[100000 + 2]; int tt = 0; void dfs(int v) { tin[v] = tt++; sz[v] = 1; for (auto el : tree[v]) { dfs(el); sz[v] += sz[el]; } tout[v] = tt++; } bool is_child(int par, int chld) { ...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; int n, m, k, q, a[((int)101 * 1000)], b[((int)101 * 1000)], lst[((int)101 * 1000)], strt[((int)101 * 1000)], fnsh[((int)101 * 1000)], sz[((int)101 * 1000)], now; vector<int> e[((int)101 * 1000)]; bool dad[((int)101 * 1000)]; void dfs(int x) { sz[x] = 1, strt[x] = ...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; long long p[(long long)(2e6 + 5)]; long long t[(long long)(2e6 + 5)]; long long ccc[(long long)(2e6 + 5)], sz[(long long)(2e6 + 5)], in[(long long)(2e6 + 5)], out[(long long)(2e6 + 5)]; vector<long long> v[(long long)(2e6 + 5)]; long long cnt; long long cc; void dfs(lon...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; const int N = (int)1e5 + 10; int n, m, k, q; vector<pair<int, int> > adj[N]; pair<int, int> request[N]; int cntRequest[N]; vector<int> own[N]; int owner[N]; int wait[N]; int deg[N], root[N]; int h[N], p[N][21]; int timer, timeIn[N], timeOut[N]; int f[N], g[N]; void Free(int...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import java.util.*; public class D417 { static int n; static int m; static int k; static int q; s...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; } void PLAY() { cout << fixed << setprecision(1...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; long long readi() { long long input = 0; char c = ' '; while (c < '-') { c = getchar(); } bool negative = false; if (c == '-') { negative = true; c = getchar(); } while (c >= '0') { input = 10 * input + (c - '0'); c = getchar(); } if ...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; int parent[100010], in[100010], out[100010], owner[100010], last[100010], size[100010]; vector<int> graph[100010]; int ncnt = 0; void dfs(int s) { in[s] = ncnt++; size[s] = 1; for (int i = 0; i < graph[s].size(); i++) { if (graph[s][i] != parent[s]) { df...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; vector<vector<int> > g; vector<int> tin, tout; vector<int> start; vector<int> _size; int timer = 0; void dfs(int v) { tin[v] = timer++; for (int i = 0; i < g[v].size(); i++) { int to = g[v][i]; dfs(to); _size[v] += _size[to]; } _size[v]++; tout[v] = ti...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; int tt = 0; int tin[100004]; int tout[100004]; int size[100004]; bool check[100004]; bool compare(int a, int b) { return tout[a] > tout[b]; } int dfs(const vector<vector<int> > &graph, int node) { check[node] = true; tin[node] = tt++; int wt = 1; for (int i = 0; i <...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; std::vector<std::vector<int> > ch; std::vector<int> vis; std::vector<int> lev; std::vector<int> home; std::vector<int> st; int curt = 0; int tin[100010], tout[100010]; void dfs(int v) { st[v] = 1; tin[v] = curt++; for (auto u : ch[v])...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; istream& in = cin; int n, m, K, Q; vector<int> vi[100010]; int toy[100010]; bool vis[100010]; int mark[100010]; int deep[100010]; void Init() { memset(toy, -1, sizeof(toy)); } void input() { in >> n >> m >> K >> Q; for (int i = 1; i <= K; ++i) { int a, b; in >> ...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; } template <typename T1, typename T2> inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; } inline int readChar(); template <class T = int> inline T readInt(); template <class T> inl...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; const int N = 100100; int i, j, k, n, m, K, q, ch, x, y, En, Pn; int f[N], h[N], now[N], last[N], z[N], l[N], r[N]; struct edge { int s, n; } E[N]; void R(int &x) { x = 0; ch = getchar(); while (ch < '0' || '9' < ch) ch = getchar(); while ('0' <= ch && ch <= '9') ...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } const int MAXN = 100000; const int MAXTOY = 100000; int n, ntoy, nreq, nq; int lastreq[MAXTOY]; int par[MAXN]; int chead[MAXN], cnxt[MAXN]; int sz[MAXN]; int tin[MAXN], tout[MAXN], nt; void dfs(i...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; int n, m, k, q, idx = 0, fa[MAXN], in[MAXN], out[MAXN], who[MAXN]; vector<int> G[MAXN]; void dfs(int u) { in[u] = ++idx; for (auto v : G[u]) dfs(v); out[u] = idx; } int main() { scanf("%d%d%d%d", &n, &m, &k, &q); for (int i = 1, x, y; i <=...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; void file() {} void fast() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); } const int N = 1e5 + 9; int n, m, tim, k, q, lastTake[N], lastNeed[N], in[N]; vector<int> adjL[N]; pair<int, i...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 100; int ar[MAXN], par[MAXN], tin[MAXN], tout[MAXN], c[MAXN]; vector<int> v[MAXN]; set<int> roots; int t = 0; void dfs(int node) { tin[node] = ++t; c[node] = 1; for (int i = 0; i < v[node].size(); i++) { dfs(v[node][i]); c[node] += c[v[n...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; const int INF = (int)1E9; int N, M, K, Q, req[100005], holder[100005]; queue<int> qs[100005]; vector<int> holds[100005]; void release(int child) { if (child == -1) return; vector<int> &v = holds[child]; for (int i = (0); i < (v.size()); i++) { int toy = v[i]; ...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; int n, m, k, q; vector<int> adjt[100010]; int holder[100010]; int notroot[100010]; int dfsin[100010]; int dfsout[100010]; int cnt[100010]; int cur = 0; int dfs(int root) { dfsin[root] = cur++; cnt[root] = 1; for (auto child : adjt[root]) { cnt[root] += dfs(child);...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5, lg = 17; int n, m, t[maxn], p[lg][maxn], s[maxn], v[maxn], d[maxn]; vector<int> e[maxn]; void dfs(int u, int up) { s[u] = 1; p[0][u] = up; d[u] = d[up] + 1; for (auto v : e[u]) if (v != up) { dfs(v, u); s[u] += s[v]; } }...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; int n, m, k, q; vector<vector<int> > g; vector<int> pr; vector<bool> rt; int cnt; vector<int> in; vector<int> out; vector<int> sz; void path(int u) { in[u] = cnt++; sz[u] = 1; for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; path(v); sz[u] += sz[v]...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007, oo = 1000000009; void Emsawy() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); } int dx[] = {1, -1, 0, 0, 1, -1, 1, -1}; int dy[] = {0, 0, 1, -1, 1, -1, -1, 1}; vector<vector<int> > adj; const long long N = 100000 + 5; int n, m, k, ...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T &x) { char c; bool nega = 0; while ((!isdigit(c = getchar())) && (c != '-')) ; if (c == '-') { nega = 1; c = getchar(); } x = c - 48; while (isdigit(c = getchar())) x = x * 10 + c - 48; if (nega) x = -x; }...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; int n, m, k, q, t; int now[100000 + 5], in[100000 + 5], out[100000 + 5], sz[100000 + 5]; vector<int> v[100000 + 5]; bitset<100000 + 5> is; int DFS(int x) { in[x] = t++; sz[x] = 1; for (int i : v[x]) sz[x] += DFS(i); out[x] = t++; return sz[x]; } int main() { cin...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> const int maxn = 1e5 + 5; int n, m, k, q; std::vector<int> g[maxn]; int in[maxn], out[maxn], vis[maxn], dfs_clock; void dfs(int u) { in[u] = ++dfs_clock; for (int i = 0; i < g[u].size(); i++) dfs(g[u][i]); out[u] = dfs_clock; } int main() { scanf("%d%d%d%d", &n, &m, &k, &q); for (int ...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; const int N = 100000; int n, m, k, q, t, in[N], out[N], sz[N], last_child[N], last_toy[N]; bool play[N]; vector<vector<int> > g; vector<pair<int, int> > tmp; void dfs(int u, int p) { in[u] = ++t; sz[u] = 1; for (int i = 0; i < g[u].size(); i++) if (g[u][i] != p) {...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; const int INF = 0x7FFFFFFF; const long long LINF = 0x7FFFFFFFFFFFFF; void guan() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } const int maxn = 1010101; const int mod = 1e9 + 7; const double eps = 1e-12; int user[maxn], son[maxn]; vector<int> g[maxn]...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; using namespace std; class DisjointSet { public: vector<int> parent; DisjointSet(int n) : parent(n) { for (int i = 0; i < n; i++) parent[i] = i; } void join(int a, int b) { parent[find(b)] = find(a); } int find(int a) { return a == parent[a] ? a : parent[a] =...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '{'; for (int i = (0), __i = (((int)(vec).size())); i < __i; i++) { os << vec[i]; if (i + 1 != ((int)(vec).size())) os << ','; } os << '}'; return os; } template <class T1, c...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; const int maxn = 100050; struct UnionSet { int s[maxn]; UnionSet(int n) { for (int i = 0; i <= n; i++) { s[i] = i; } } int gets(int x) { if (s[x] == x) return x; else return s[x] = gets(s[x]); } void merge(int x, int y) { in...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; const int inf = 1000000000; const int mod = 1000000007; const int sze = 100005; int n, m, k, q; vector<int> graphs[sze]; bool isroot[sze]; int whosepossion[sze]; int tin[sze], tout[sze]; int gltime = 1; int szsubtree[sze]; void dfs(int node) { tin[node] = gltime++; szsu...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; const int MX = 1 * 1e5 + 10; const double PI = acos(-1.0), EPS = 1e-9; int N, M, k, q; vector<vector<int> > G(MX); vector<pair<int, int> > edge; int toy[MX]; int child[MX]; int sz[MX]; int is_root[MX]; int in[MX], out[MX]; int t = 0; void dfs(int x, int p) { in[x] = t++; ...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; const int N = 100004; int st[N], fn[N], vis[N], par[N], sz[N]; vector<int> g[N]; int t = 0; void dfs(int u) { int i, v; st[u] = ++t; sz[u] = 1; for (i = 0; i < g[u].size(); i++) { v = g[u][i]; dfs(v); sz[u] += sz[v]; } fn[u] = ++t; } int main() { i...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; const int MaxN = 100005; vector<int> G[MaxN], euler = {-1}; int n, m, k, q; int lastWaiting[MaxN], subtreeSize[MaxN], firstAppar[MaxN], lastAppar[MaxN], inDeg[MaxN]; void FindSubtreeSize(int vertex) { euler.push_back(vertex); for (int nxt : G[vertex]) { FindSubt...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; vector<int> way[100005]; int last[100005], in[100005]; int t_in[100005], t_out[100005]; int id[100005], sz[100005]; int T; int dfs(int pos, int comp) { id[pos] = comp; t_in[pos] = ++T; sz[pos] = 1; for (auto child : way[pos]) sz[pos] += dfs(child, comp); t_out[pos...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 100; int who[N], in[N], out[N], sz[N], root[N]; vector<vector<int> > G(N); int t = 1; void dfs(int u) { sz[u] = 1; in[u] = ++t; for (auto v : G[u]) { dfs(v); sz[u] += sz[v]; } out[u] = t - 1; } bool isParent(int u, int v) { return in[u]...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; vector<int> G[N]; int f[N], belong[N], siz[N], cnt, in[N], out[N]; void add(int x, int y) { G[x].push_back(y); f[y] = x; } void dfs(int u, int x, int d) { in[u] = ++cnt; siz[u] = 1; for (int i = 0; i < G[u].size(); i++) { int v = G[u][i...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
import java.io.*; import java.util.*; public class A { static ArrayList<Integer>[] adj; static int[] tin, tout, cc, sub; static int timer; static boolean isAnc(int u, int v) { return tin[u] >= tin[v] && tin[u] <= tout[v]; } static boolean inSameTree(int u, int v) { return cc[u] == cc[v] && (isAnc(u, v) |...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; long long n, m, k, q, a, b, x, y; long long t1, t2; bool freee[100111]; long long owner[100111]; long long par[100111]; vector<long long> son[100111]; long long sizedown[100111]; long long tin[100111]; long long tout[100111]; long long ans; long long dfs(long long v) { si...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using uint = unsigned int; using ull = unsigned long long; template <typename T> using pair2 = pair<T, T>; using pii = pair<int, int>; using pli = pair<ll, int>; using pll = pair<ll, ll>; const int N = 1e5 + 40; vector<int> g[N]...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; using ll = long long int; using ld = long double; using pi = pair<int, int>; const double PI = acos(-1.0); const double eps = 1e-9; const ll mod = 1e9 + 7; const int inf = 1e7; const int MAXN = 1e5 + 5; bool vis[MAXN]; int subtree[MAXN]; int tin[MAXN], tout[MAXN]; int TIMER...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; void io() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(15); } const int MAX = 1e5 + 5; int lst_req[MAX]; int par[MAX]; vector<int> g[MAX]; int n, m, k, q; int tin[MAX]; int tout[MAX]; int pool; int subsz[MAX]; void dfs(int u) { subsz...
Problem: Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys: * Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing i...
#include <bits/stdc++.h> using namespace std; template <typename A> ostream &operator<<(ostream &cout, vector<A> const &v); template <typename A, typename B> ostream &operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.first << ", " << p.second << ")"; } template <typename A> ostream &operator<<(...