solution
stringlengths
11
983k
difficulty
int64
0
21
language
stringclasses
2 values
n=int(input()) a=list(map(int,input().split())) c=0 for i in range(1,n): if a[i]> max(a[0:i]) or a[i]< min(a[0:i]): c=c+1 print(c)
7
PYTHON3
input() max_s = -1 min_s = 10001 counter = -2 for n in map(int, input().split()): if n > max_s: max_s = n counter += 1 if n < min_s: min_s = n counter += 1 print(counter)
7
PYTHON3
x=int(input()) a=list(map(int,input().split())) maxi=a[0] mini=a[0] c=0 for i in a: if i > maxi: maxi=i c+=1 elif i<mini: mini=i c+=1 print(c)
7
PYTHON3
# Sourav Basak Shuvo // KUET BME'18 n = int(input()) x = [int(i) for i in input().split()] min_n = max_n = x[0] k = 0 for i in range(1, len(x)): if x[i] > max_n: k += 1 max_n = x[i] if x[i] < min_n: k += 1 min_n = x[i] print(k)
7
PYTHON3
n = int(input()) x = [int(i) for i in input().split()] mini = x[0] maxi = x[0] c = 0 for i in range(1, len(x)): if(mini>x[i]): c+=1 mini = x[i] if(maxi<x[i]): c+=1 maxi = x[i] print(c)
7
PYTHON3
n=int(input()) x=[int(x) for x in input().split()] count=0 m=x[0] l=x[0] for i in range(1,n): if x[i]>m: count+=1 m=x[i] elif x[i]<l: count+=1 l=x[i] print(count)
7
PYTHON3
n = int(input()) scores = [int(x) for x in input().split()] count = 0 for x in range(len(scores)): if (all(y > scores[x] for y in scores[0:x]) or all(y < scores[x] for y in scores[0:x])) and len(scores[0:x]) != 0: count += 1 print(count)
7
PYTHON3
n = int(input()) s = list(map(int, input().split())) count_of_points = 0 big_amazing_competition = s[0] small_amazing_competition = s[0] for i in s: if i > big_amazing_competition: big_amazing_competition = i count_of_points += 1 if i < small_amazing_competition: small_amazing_competition = i count_of_points += 1 print(count_of_points)
7
PYTHON3
n = int(input()) contest = input().split() contest = [int(i) for i in contest] j = 1 count = 0 while j < len(contest): if contest[j] > max(contest[0:j]) or contest[j] < min(contest[0:j]): count +=1 j += 1 else: j += 1 print(count)
7
PYTHON3
import sys it = iter(sys.stdin.read().splitlines()) n = int(next(it)) results = [int(x) for x in next(it).split(" ")] best = results.pop(0) worst = best counter = 0 for result in results: if result > best: best = result counter += 1 if result < worst: worst = result counter += 1 print(counter)
7
PYTHON3
def main(): n_contests = int(input()) scores = [int(_) for _ in input().split()] min_score = max_score = scores[0] amazing_count = 0 for i in range(1, n_contests): if scores[i] < min_score: min_score = scores[i] amazing_count += 1 elif scores[i] > max_score: max_score = scores[i] amazing_count += 1 print(amazing_count) if __name__ == '__main__': main()
7
PYTHON3
n = input() a = [int(x) for x in input().split()] min = a[0] max = a[0] c = 0 for i in a : if i > max: max = i c = c +1 if i < min: min = i c = c +1 print(c)
7
PYTHON3
n = int(input()) line =[int(x) for x in input().split()] big = line[0] small = line[0] count=0 for i in range(1,n): if line[i] > big: count+=1 big = line[i] if line[i] < small: count+=1 small = line[i] print(count)
7
PYTHON3
a=int(input()) b=input() c=b.split() i=1 min=int(c[0]) max=int(c[0]) amazing=0 while i<a: if int(c[i])>max: amazing+=1 max=int(c[i]) elif int(c[i])<min: amazing+=1 min=int(c[i]) i+=1 print(amazing)
7
PYTHON3
n=int(input()) a=[int(i) for i in input().split()] maxm=a[0] minm=a[0] ans=0 for i in a: if(i>maxm): ans+=1 maxm=i elif(i<minm): ans+=1 minm=i print(ans)
7
PYTHON3
n=int(input()) a=list(map(int,input().strip().split())) mini=a[0] maxi=a[0] c=0 for i in range(1,n): if a[i]>maxi: maxi=a[i] c+=1 elif a[i]<mini: mini=a[i] c+=1 print(c)
7
PYTHON3
n = int(input()) scores = list(map(int, input().split())) best = scores[0] worst = scores[0] amazing = 0 for i in range(1, n): if(scores[i] > best): amazing += 1 best = scores[i] elif(scores[i] < worst): amazing += 1 worst = scores[i] print(amazing)
7
PYTHON3
n=int(input()) l=list(map(int,input().split())) max=l[0] min=l[0] m=0 for i in range(1,n): if l[i]>max: m=m+1 max=l[i] elif l[i]<min: m=m+1 min=l[i] print(m)
7
PYTHON3
n = int(input()) #n, m = map(int, input().split()) #s = input() l = 0 c = list(map(int, input().split())) m = c[0] k = c[0] for i in range(1, n): if c[i] > m: l += 1 m = c[i] elif c[i] < k: l += 1 k = c[i] print(l)
7
PYTHON3
n = int(input()) x = [int(i) for i in input().split()] max = x[0] min = x[0] z = 0 for i in x: if i > max: max = i z += 1 elif i < min: min = i z += 1 print(z)
7
PYTHON3
n = int(input()) performance = list(map(int, input().split())) min_res = performance[0] max_res = performance[0] wonders = 0 for i in range(1, n): if performance[i] > max_res: max_res = performance[i] wonders += 1 elif performance[i] < min_res: min_res = performance[i] wonders += 1 print(wonders)
7
PYTHON3
n = int(input()) arr = list(map(int,input().split()[:n])) min = 0 max = 0 c = 0 for i in range(1,n): if arr[min] < arr[i]: min = i c += 1 if arr[max] > arr[i]: max = i c += 1 print(c)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; int ans = 0, maxm = arr[0], minm = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] > maxm) { maxm = arr[i]; ans++; } if (arr[i] < minm) { minm = arr[i]; ans++; } } cout << ans << endl; }
7
CPP
def main(n, scores): min_score = scores[0] max_score = scores[0] amaz_perf = 0 for i in range(1, n): if scores[i] < min_score: min_score = scores[i] amaz_perf += 1 if scores[i] > max_score: max_score = scores[i] amaz_perf += 1 return amaz_perf if __name__ == "__main__": n = int(input().strip()) scores = list(map(int, input().strip().split())) print(main(n, scores))
7
PYTHON3
n=int(input()) points=list(map(int,input().split())) a,b,c=points[0],points[0],0 for i in range(1,n): if points[i]>a or points[i]<b: c+=1 if points[i]>a: a=points[i] else: b=points[i] else: continue print(c)
7
PYTHON3
n=int(input()) a=input().split() maxx=int(a[0]) minn=int(a[0]) count=0 for i in range(1,n): if int(a[i])<minn: count=count+1 minn=int(a[i]) elif int(a[i])>maxx: count=count+1 maxx=int(a[i]) print(count)
7
PYTHON3
n=input() l=input().split() ans=0 best=int(l[0]) worst=int(l[0]) for i in range(1,len(l)): if(int(l[i])>best): ans+=1 best=int(l[i]) if(int(l[i])<worst): ans+=1 worst=int(l[i]) print(ans)
7
PYTHON3
n=int(input()) l=list(map(int,input().split())) count=0 min=l[0] max=l[0] for i in range(n): if l[i]>max: max=l[i] count+=1 if l[i]<min: min=l[i] count+=1 print(count)
7
PYTHON3
n = int(input()) arr = [int(x) for x in input().split()] count = 0 max = arr[0] min = arr[0] for i in range(1, n): if arr[i] > max: max = arr[i] count += 1 if arr[i] < min: min = arr[i] count += 1 print(count)
7
PYTHON3
n=int(input()) l=list(map(int,input().split())) res=0 for i in range (1,n,1): k=m=0 for j in range(0,i,1): if l[i]>l[j]: k=k+1 if l[i]<l[j]: m=m+1 if k==i or m==i: res=res+1 if res==0: print(0) else: print(res)
7
PYTHON3
n = int(input()) a = list(map(int, input().split(' '))) count = 0 mn = mx = a[0] for i in range(1, n): if a[i] > mx: count += 1 mx = a[i] elif a[i] < mn: count += 1 mn = a[i] else: pass print(count)
7
PYTHON3
n=int(input()) arr=[int(x) for x in input().split()] mx,mn,c=0,0,0 mx=arr[0] mn=arr[0] for i in range(n): if arr[i]>mx: mx=arr[i] c+=1 elif arr[i]<mn: c+=1 mn=arr[i] print(c) #MADAF SUM IS POORELY WORDED
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> ratings(n); for (int i = 0; i < n; i++) cin >> ratings[i]; int w = ratings[0], b = ratings[0], count = 0; for (int i = 1; i < n; i++) { if (ratings[i] > b) { b = ratings[i]; count++; } else if (ratings[i] < w) { w = ratings[i]; count++; } } cout << count; }
7
CPP
n=int(input()) s=input().split(" ") a=[0]*n for i in range(n): a[i]=int(s[i]) mx=a[0] mn=a[0] amazing=0 for i in range(1,n): if a[i]>mx: mx=a[i] amazing+=1 elif a[i]<mn: mn=a[i] amazing+=1 print(amazing)
7
PYTHON3
n=int(input()) amazing=0 list=list(map(int,input().strip().split()))[0:n] a=list[0] b=list[0] for i in range (1,n): if list[i]<b: b=list[i] amazing=amazing+1 if list[i]>a: a=list[i] amazing=amazing+1 print(amazing)
7
PYTHON3
n = int(input()) mas = [int(s) for s in input().split()] counter = 0 maxim = minim = mas[0] for i in range(1, len(mas)): if mas[i] > maxim: counter += 1 maxim = mas[i] if mas[i] < minim: counter += 1 minim = mas[i] print(counter)
7
PYTHON3
n = int(input()) nums = list(map(int, input().split())) count = 0 for i in range(1, n): j = 0 greater = False smaller = False fail = False while j < i: if nums[j] >= nums[i]: if smaller: fail = True break else: greater = True if nums[j] <= nums[i]: if greater: fail = True break else: smaller = True j += 1 if not fail: count += 1 print(count)
7
PYTHON3
input();x=0 l=list(map(int,input().split())) a=max(l[0:2]) b=min(l[0:2]) if a!=b: x+=1 for i in range(2,len(l)): if l[i]>a : x+=1 a=l[i] if l[i]<b: x+=1 b=l[i] print(x)
7
PYTHON3
n,arr = int(input()),list(map(int,input().split(' '))) print(sum(not min(arr[:i])<=arr[i]<=max(arr[:i]) for i in range(1,n)))
7
PYTHON3
n = int(input()) s = input().split() s = [int(x) for x in s] if n==2 and s[0]==s[1]: print(0) elif n<=2: print(n-1) else: M = max(s[0],s[1]) N = min(s[0],s[1]) if M == N: total = 0 else: total = 1 for i in range(2,n): if s[i] > M: M = s[i] total +=1 elif s[i] < N: N = s[i] total +=1 print(total)
7
PYTHON3
n=int(input()) a=[int(x) for x in input().split()] da=a[0] xiao=a[0] num=0 for i in range(1,n): if a[i]>da: num+=1 da=a[i] elif a[i]<xiao: num+=1 xiao=a[i] print(num)
7
PYTHON3
n = int(input()) a, counter = [int(i) for i in input().split()], 0 for i in range(1, n): counter += a[i] < min(a[:i]) or a[i] > max(a[:i]) print(counter)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<int> x; int main() { int m, l; cin >> m; for (int i = 0; i < m; i++) { cin >> l; x.push_back(l); } int big = x[0], small = x[0], c = 0; for (int i = 0; i < m; i++) { if (x[i] > big) { c++; big = x[i]; } else if (x[i] < small) { c++; small = x[i]; } } cout << c; }
7
CPP
#include <bits/stdc++.h> int main(void) { int n, best, worst, current, answ = 0; scanf("%d", &n); scanf("%d", &current); best = worst = current; for (n--; n--;) { scanf("%d", &current); if (current < worst) { worst = current; answ++; } if (current > best) { best = current; answ++; } } printf("%d\n", answ); return 0; }
7
CPP
n=int(input()) l=list(map(int,input().split())) max = l[0] min = l[0] c=0 for i in l: if(i>max): c+=1 max=i elif(i<min): c+=1 min=i print(c)
7
PYTHON3
n=int(input()) count=0 a=[int(i) for i in input().split()] for i in range(1,n): m=max(a[0:i]) m_=min(a[0:i]) if (a[i]>m) or (a[i]<m_): count+=1 print(count)
7
PYTHON3
n = int(input()) points = input().split() list1 = [int(points[0])] ans = 0 for i in range(1,n): x = points[i] y = max(list1) z = min(list1) if int(points[i]) > int(max(list1)) or int(points[i]) < int(min(list1)): ans += 1 list1.append(int(points[i])) print(ans)
7
PYTHON3
n = int(input()) a = list(map(int,input().strip().split()))[:n] maxNum = a[0] minNum = a[0] count = 0 for i in range(n): if a[i] > maxNum or a[i] < minNum: count += 1 if a[i] > maxNum: maxNum = a[i] elif a[i] < minNum: minNum = a[i] print(count)
7
PYTHON3
n = int(input()) arr = input().split() minimal, maximum = int(arr[0]), int(arr[0]) cnt = 0 for i in range(1, n): if int(arr[i]) > maximum: cnt += 1 maximum = int(arr[i]) elif int(arr[i]) < minimal: cnt += 1 minimal = int(arr[i]) print(cnt)
7
PYTHON3
import sys import os from io import IOBase, BytesIO # import heapq import math # import collections # import itertools # import bisect mod = 10 ** 9 + 7 pie = 3.1415926536 # import resource # resource.setrlimit(resource.RLIMIT_STACK, [0x100000000, resource.RLIM_INFINITY]) # import threading # threading.stack_size(2**27) # import sys # sys.setrecursionlimit(10**6) # fact=[1] # for i in range(1,1000001): # fact.append((fact[-1]*i)%mod) # ifact=[0]*1000001 # ifact[1000000]=pow(fact[1000000],mod-2,mod) # for i in range(1000000,0,-1): # ifact[i-1]=(i*ifact[i])%mod # from random import randint as rn # from Queue import Queue as Q def modinv(n, p): return pow(n, p-2, p) def ncr(n, r, p): # for using this uncomment the lines calculating fact and ifact t = ((fact[n])*((ifact[r]*ifact[n-r]) % p)) % p return t def ain(): # takes array as input return list(map(int, sin().split())) def sin(): return input().strip() def GCD(x, y): while(y): x, y = y, x % y return x def read2DIntArray(row, col): arr = [] for i in range(0, row): temp = list(map(int, sin().split())) arr.append(temp) return arr def read2DCharArray(row, col): arr = [] for i in range(0, row): temp = str(sin()) arr.append(temp) return arr """****************** SMALLEST NO. BY REARRANGING DIGITS OF n (WITHOUT TRAILING ZEROS) *********************""" def smallestNumber(n): lst = list(str(n)) lst.sort() tmp = "" for i, n in enumerate(lst): if (n != '0'): tmp = lst.pop(i) break return str(tmp) + ''.join(lst) """*********************** GENERATE ALL PRIME NUMBERS SMALLER THAN OR EQUAL TO n ***************************""" def SieveOfEratosthenes(n): prime = [True for i in range(n + 1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * 2, n + 1, p): prime[i] = False p += 1 prime[0] = False prime[1] = False lst = [] for p in range(n + 1): if prime[p]: lst.append(p) return lst """*************************************** FIND nCr ********************************************************""" def nCr(n, r): a = 1 b = 1 c = 1 for i in range(1, n + 1): c *= i for i in range(1, r + 1): b *= i for i in range(1, n - r + 1): a *= i return (c // (a * b)) """************************* GET PRIME FACTORS AND THEIR POWERS FOR AN INTEGER *****************************""" def sieveOfEratosthenes1(N, s): prime = [False] * (N+1) for i in range(2, N+1, 2): s[i] = 2 for i in range(3, N+1, 2): if (prime[i] == False): s[i] = i for j in range(i, int(N / i) + 1, 2): if (prime[i*j] == False): prime[i*j] = True s[i * j] = i def generatePrimeFactors(N): s = [0] * (N+1) sieveOfEratosthenes1(N, s) # print("Factor Power") curr = s[N] cnt = 1 factors = [] power = [] while (N > 1): N //= s[N] if (curr == s[N]): cnt += 1 continue # curr is factor and cnt in the power of this factor factors.append(curr) power.append(cnt) curr = s[N] cnt = 1 return factors, power """----------------------------------------------MAIN------------------------------------------------------""" def main(): maxnum = -1 minnum = 10001 ans = 0 n = int(sin()) arr = ain() for i in range(n): num = arr[i] if i > 0: if num > maxnum or num < minnum: ans += 1 maxnum = max(num, maxnum) minnum = min(num, minnum) print(ans) """--------------------------------------------------------------------------------------------------------""" # Python 2 and 3 footer by Pajenegod and c1729 py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange 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) def input(): return sys.stdin.readline().rstrip('\r\n') if __name__ == '__main__': main() # threading.Thread(target=main).start()
7
PYTHON3
hm = int(input()) numbers = input().split(" ") lis = [] amazing = 0 flag = 0 for i in range(hm): if(hm == 1): flag = 1 print(0) break if (i == 0): numbers[i] = int(numbers[i]) lis.append(numbers[i]) continue numbers[i] = int(numbers[i]) if numbers[i] > max(lis): amazing += 1 if numbers[i] < min(lis): amazing += 1 lis.append(numbers[i]) if flag == 0: print(amazing)
7
PYTHON3
n = int(input()) points = list(map(int, input().split(' '))) maxi = points[0] less = points[0] count = 0 for p in points: if p > maxi: maxi = p count += 1 elif p < less: less = p count += 1 print(count)
7
PYTHON3
#!/usr/bin/env python # coding: utf-8 # In[204]: # # n = int(input()) # # line = list(map(int, input().split())) # In[480]: n = int(input()) num_list = list(map(int, input().split())) # In[483]: records = [num_list[0], num_list[0]] count = 0 for i in num_list[1:]: if i < records[0]: count += 1 records[0] = i elif i > records[1]: count += 1 records[1] = i # In[484]: print(count) # In[ ]:
7
PYTHON3
n=int(input()) a=list(map(int, input().strip().split(' '))) ans=0 min=a[0] max=a[0] for i in a[1:]: if(i<min): min=i ans+=1 elif(i>max): max=i ans+=1 print (ans)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, point, count = 0; cin >> n; cin >> point; int min = point, max = point; for (int i = 1; i < n; i++) { cin >> point; if (point > max) { max = point; count++; } else if (point < min) { min = point; count++; } } cout << count; return 0; }
7
CPP
n = int(input()) arr = [int(x) for x in input().split()] i = 1 mx = arr[0] mn = arr[0] count = 0 while i < n: if arr[i] < mn: mn = arr[i] count += 1 if arr[i] > mx: mx = arr[i] count += 1 i += 1 print(count)
7
PYTHON3
n = int(input()) mini = 10**6 maxi = -1 tt = 0 for indice, i in enumerate(map(int, input().split())): if indice: if i < mini or i > maxi: tt += 1 mini = min(mini, i) maxi = max(maxi, i) print(tt)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long N_max = 0; class graph { private: long long V; long long k, m; vector<pair<long long, long long>> L; vector<pair<long long, long long>> L2; public: graph(long long n) { V = n; } void add_edge(long long u, long long v) { L.push_back({u, v}); } long long find_set(long long i, long long parent[]) { if (parent[i] == -1) return i; parent[i] = find_set(parent[i], parent); return find_set(parent[i], parent); } void union_set(long long x, long long y, long long parent[], long long rank[]) { long long S1 = find_set(x, parent); long long S2 = find_set(y, parent); if (S1 != S2) { if (rank[S1] < rank[S2]) { parent[S1] = S2; rank[S2] += rank[S1]; } else { parent[S2] = S1; rank[S1] += rank[S2]; } } } void solve() { long long m, i, x, y, u, v, ans, leader, sz; long long parent[V]; long long rank[V]; bool isValid[V]; for (i = 0; i < V; i++) { parent[i] = -1; rank[i] = 1; isValid[i] = true; } for (auto edge : L) { u = edge.first; v = edge.second; union_set(u, v, parent, rank); } cin >> m; for (i = 0; i < m; i++) { cin >> u >> v; u -= 1, v -= 1; x = find_set(u, parent); y = find_set(v, parent); if (x == y) { isValid[x] = false; } } ans = 0; for (i = 0; i < V; i++) { leader = find_set(i, parent); if (isValid[leader]) { sz = rank[leader]; ans = max(ans, sz); } } cout << ans << "\n"; } }; void run_case() { long long n, i, k, u, v; cin >> n; graph G(n); cin >> k; for (i = 0; i < k; i++) { cin >> u >> v; u -= 1, v -= 1; G.add_edge(u, v); } G.solve(); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long T = 1; while (T--) run_case(); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int Belong[2009]; int Cnt[2009]; bool f[2009]; int find(int x) { int j, t = x; while (x != Belong[x]) { x = Belong[x]; } while (t != Belong[t]) { j = Belong[t]; Belong[t] = x; t = j; } return x; } void merge(int x, int y) { int xx = find(x), yy = find(y); if (xx != yy) { Belong[xx] = yy; Cnt[yy] += Cnt[xx]; } } int main() { int n, m; scanf("%d", &n); for (int i = 1; i <= n; ++i) Belong[i] = i, Cnt[i] = 1; scanf("%d", &m); int u, v; while (m--) { scanf("%d%d", &u, &v); merge(u, v); } scanf("%d", &m); int x, y; memset(f, false, sizeof(f)); while (m--) { scanf("%d%d", &u, &v); x = find(u); y = find(v); if (x == y) f[x] = true; } int ret = 0; for (int i = 1; i <= n; ++i) { if (Belong[i] == i && !f[i]) ret = max(ret, Cnt[i]); } printf("%d\n", ret); return 0; }
9
CPP
#include <bits/stdc++.h> template <typename T, typename U> static inline void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> static inline void amax(T &x, U y) { if (x < y) x = y; } using namespace std; const int N = 2005; int par[N], r[N]; int find(int a) { if (par[a] < 0) return a; else return (par[a] = find(par[a])); } void merge(int a, int b) { a = find(a); b = find(b); if (a == b) return; if (r[a] > r[b]) { par[b] = a; r[a] += r[b]; } else { par[a] = b; r[b] += r[a]; } } void solve() { int n, m, k, a, b, i; cin >> n >> k; for (i = 1; i < n + 1; i++) par[i] = -1, r[i] = 1; for (i = 0; i < k; i++) { cin >> a >> b; merge(a, b); } cin >> m; if (k == 0 && m != 0) { for (i = 0; i < m; i++) { cin >> a >> b; merge(a, b); } int cnt = 0; for (i = 1; i < n + 1; i++) if (par[i] < 0) cnt++; cout << cnt; return; } set<pair<int, int> > s; for (i = 1; i < n + 1; i++) { if (r[i] > 1) s.insert(make_pair(r[i], i)); if (r[i] == 1 && par[i] < 0) s.insert(make_pair(r[i], i)); } for (i = 0; i < m; i++) { cin >> a >> b; if (find(a) == find(b)) { int p = find(a); for (auto i : s) { if (i.second == p) { s.erase(i); break; } } } } if (k == 0 && m == 0) cout << 1; else if ((int)s.size()) { auto it = s.end(); it--; cout << it->first; } else cout << 0; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) { solve(); } return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; vector<int> V[3000]; int vis[3000]; int k = 1; int siz[3000]; void dfs(int x) { for (int i = 0; i < V[x].size(); i++) { if (vis[V[x][i]] == 0) { vis[V[x][i]] = k; siz[k]++; dfs(V[x][i]); } } } int main() { int n, kk, m; int x, y; cin >> n; cin >> kk; for (int i = 0; i < kk; i++) { cin >> x >> y; V[x].push_back(y); V[y].push_back(x); } for (int i = 1; i <= n; i++) if (vis[i] == 0) { siz[k]++; vis[i] = k; dfs(i); k++; } cin >> m; for (int i = 0; i < m; i++) { cin >> x >> y; if (vis[x] == vis[y]) siz[vis[x]] = 0; } sort(siz, siz + n); reverse(siz, siz + n); int ans = 0; for (int i = 0; siz[i] != 0; i++) ans = max(ans, siz[i]); cout << ans << endl; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int mxn = 2000 + 10; vector<int> v[mxn]; vector<pair<int, int> > v2; int n, m; int mx; bool viz[mxn]; int dfs(int nod) { int nr = 0; stack<int> s; s.push(nod); viz[nod] = 1; int nx; while (!s.empty()) { nx = s.top(); s.pop(); nr++; for (auto it : v[nx]) if (viz[it] == 0) { s.push(it); viz[it] = 1; } } bool ok = 1; for (int i = 0; i < v2.size(); i++) { if (viz[v2[i].first] && viz[v2[i].second]) { ok = 0; v2.erase(v2.begin() + i); } } if (nr == 1) return 1; return nr * ok; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 1, xx, yy; i <= m; i++) { cin >> xx >> yy; v[xx].push_back(yy); v[yy].push_back(xx); } cin >> m; for (int i = 1, xx, yy; i <= m; i++) { cin >> xx >> yy; v2.push_back(make_pair(xx, yy)); } sort(v2.begin(), v2.end(), less<pair<int, int> >()); for (int i = 1; i <= n; i++) if (viz[i] == 0) mx = max(mx, dfs(i)); cout << mx; return 0; }
9
CPP
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse4") using namespace std; int gcd(int a, int b) { return ((b == 0) ? a : gcd(b, a % b)); } struct DSU { int S; struct node { int parent; long long sum; }; vector<node> dsu; DSU(int n) { S = n; for (int i = 0; i < (int)(n); i++) { node tmp; tmp.parent = i; tmp.sum = 1; dsu.push_back(tmp); } } void reset(int n) { dsu.clear(); S = n; for (int i = 0; i < (int)(n); i++) { node tmp; tmp.parent = i; tmp.sum = 1; dsu.push_back(tmp); } } int root(int u) { if (dsu[u].parent == u) return u; dsu[u].parent = root(dsu[u].parent); return dsu[u].parent; } void merge(int u, int v) { u = root(u); v = root(v); if (u == v) return; if (getsum(u) < getsum(v)) swap(u, v); dsu[v].parent = u; dsu[u].sum += dsu[v].sum; } bool sameset(int u, int v) { if (root(u) == root(v)) return true; return false; } long long getsum(int u) { return dsu[root(u)].sum; } }; int bad[100005]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int k; cin >> k; DSU dsu(n); memset(bad, 0, sizeof(bad)); for (int i = 0; i < (int)(k); i++) { int a, b; cin >> a >> b; a--, b--; dsu.merge(a, b); } int m; cin >> m; for (int i = 0; i < (int)(m); i++) { int u, v; cin >> u >> v; u--, v--; if (dsu.root(u) == dsu.root(v)) bad[dsu.root(u)] = 1; } long long ans = 0; for (int i = 0; i < (int)(n); i++) if (dsu.root(i) == i && !bad[dsu.root(i)]) ans = max(ans, dsu.getsum(i)); cout << ans << '\n'; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int q(vector<vector<int> >& a, vector<int>& s, vector<int>& v, int i) { int an = 1; s.push_back(i); v[i] = 1; for (int j = 0; j < a[i].size(); j++) if (v[a[i][j]] == 0) an += q(a, s, v, a[i][j]); return an; } int main() { int n, f, d; cin >> n >> f; vector<vector<int> > a(n); for (int i = 0; i < f; i++) { int s, e; cin >> s >> e; a[s - 1].push_back(e - 1); a[e - 1].push_back(s - 1); } cin >> d; vector<vector<int> > e(n, vector<int>(n)); for (int i = 0; i < d; i++) { int s, ed; cin >> s >> ed; e[s - 1][ed - 1] = e[ed - 1][s - 1] = 1; } vector<int> v(n); int an = 0; for (int i = 0; i < n; i++) { if (v[i] == 1) continue; vector<int> s; int t = q(a, s, v, i), fl = 0; for (int j = 0; j < t; j++) { for (int k = j + 1; k < t; k++) { if (e[s[j]][s[k]]) { fl = 1; break; } } if (fl) break; } if (fl == 0) an = max(an, t); } cout << an << endl; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 2050; int p1[maxn]; int find(int x, int *p) { return p[x] == x ? x : p[x] = find(p[x], p); } void un(int a, int b, int *p) { p[find(a, p)] = find(b, p); } bool atOneSet(int a, int b, int *p) { return find(a, p) == find(b, p); } set<pair<int, int> > p2; int main(void) { ios::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < (n); ++i) p1[i] = i; int ct; cin >> ct; while (ct--) { int a, b; cin >> a >> b; --a; --b; un(a, b, p1); } cin >> ct; while (ct--) { int a, b; cin >> a >> b; --a; --b; p2.insert(make_pair(a, b)); p2.insert(make_pair(b, a)); } set<int> s; map<int, int> mp; for (int i = 0; i < (n); ++i) s.insert(find(i, p1)); ct = 0; for (auto it = (s).begin(); it != (s).end(); ++it) mp[*it] = ct++; vector<vector<int> > vec(s.size()); for (int i = 0; i < (n); ++i) vec[mp[find(i, p1)]].push_back(i); int ans = 0; for (int i = 0; i < (ct); ++i) { bool tag = true; for (auto a = (vec[i]).begin(); a != (vec[i]).end(); ++a) for (auto b = (vec[i]).begin(); b != (vec[i]).end(); ++b) if (*a != *b) { if (p2.count(make_pair(*a, *b))) tag = false; } if (tag) ans = max(ans, (int)vec[i].size()); } cout << ans << endl; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; short fre[2001]; int main(int argc, const char* argv[]) { int n, k, m; cin >> n; memset(fre, -1, 2001); cin >> k; while (k--) { int i, j; cin >> i >> j; while (fre[j] > 0) { j = fre[j]; } while (fre[i] > 0) { i = fre[i]; } if (i != j) { fre[j] = i; } } for (int i = 1; i <= n; i++) { while (fre[fre[i]] > 0) { fre[i] = fre[fre[i]]; } } for (int i = 1; i <= n; i++) { if (fre[i] > 0) { fre[fre[i]]--; } } cin >> m; while (m--) { int i, j; cin >> i >> j; if (fre[i] == fre[j]) fre[fre[i]] = 0; } int min = 0; for (int i = 1; i <= n; i++) { if (fre[i] < min) { min = fre[i]; } } cout << -min; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; std::vector<vector<int> > g; vector<bool> vis; vector<bool> fal; set<pair<int, int> > mp; int cnt = 0; void edge(int x, int y) { g[x].push_back(y); g[y].push_back(x); } bool bfs(int s) { queue<int> qt; qt.push(s); vis[s] = true; fal[s] = true; while (!qt.empty()) { int s = qt.front(); qt.pop(); cnt++; for (auto x : g[s]) { if (!vis[x]) { vis[x] = true; fal[x] = true; qt.push(x); } } } for (auto it = mp.begin(); it != mp.end(); it++) { int y = it->first; int z = it->second; if (fal[y] == true && fal[z] == true) { return false; } } return true; } int main() { int n; cin >> n; g.assign(n + 1, vector<int>()); vis.assign(n + 1, false); int k; cin >> k; for (int i = 0; i < k; i++) { int x, y; cin >> x >> y; edge(x, y); } int m; cin >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; mp.insert(make_pair(x, y)); } int mx = 0; for (int i = 1; i <= n; i++) { if (!vis[i]) { fal.assign(n + 1, false); bool z = bfs(i); if (z == true) mx = max(mx, cnt); cnt = 0; } } cout << mx << endl; }
9
CPP
#include <bits/stdc++.h> using namespace std; vector<int> p[2010]; int a[2010][2010], b[2010][2010]; int vis[2010], n, m; void dfs(int x, int y) { int i, j, k; for (i = 1; i <= n; i++) if (a[x][i] && vis[i] == 0) { vis[i] = y; dfs(i, y); } } int main() { int i, j, k, x, y, ans, flag, s; scanf("%d%d", &n, &m); for (i = 1; i <= m; i++) { scanf("%d%d", &x, &y); a[x][y] = a[y][x] = 1; } scanf("%d", &m); for (i = 1; i <= m; i++) { scanf("%d%d", &x, &y); b[x][y] = b[y][x] = 1; } memset(vis, 0, sizeof(vis)); k = 1; for (i = 1; i <= n; i++) { if (vis[i]) continue; vis[i] = k; dfs(i, k); k++; } ans = 0; for (i = 1; i <= n; i++) p[vis[i]].push_back(i); for (i = 1; i < k; i++) { j = p[i].size(); flag = 1; for (x = 0; x < j; x++) { for (y = x + 1; y < j; y++) if (b[p[i][x]][p[i][y]]) { flag = 0; break; } if (flag == 0) break; } if (flag) ans = max(ans, j); } printf("%d\n", ans); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; vector<int> grafo[2000]; set<int> nemici[2000]; int rapp[2000]; list<int> liste[2000]; int N, k, m, maxi = 0; int main(void) { cin >> N; cin >> k; for (int i = 0; i < k; i++) { int da, a; cin >> da >> a; da--; a--; grafo[da].push_back(a); grafo[a].push_back(da); } cin >> m; for (int i = 0; i < m; i++) { int da, a; cin >> da >> a; da--; a--; nemici[da].insert(a); nemici[a].insert(da); } for (int i = 0; i < N; i++) { rapp[i] = i; liste[i].push_back(i); } for (int i = 0; i < N; i++) for (int k = 0; k < grafo[i].size(); k++) { int a = grafo[i][k]; if (rapp[i] != rapp[a]) { int rpicc, rgrand; if (liste[rapp[i]].size() <= liste[rapp[a]].size()) { rpicc = rapp[i]; rgrand = rapp[a]; } else { rpicc = rapp[a]; rgrand = rapp[i]; } for (list<int>::iterator it = liste[rpicc].begin(); it != liste[rpicc].end(); it++) rapp[*it] = rgrand; liste[rgrand].merge(liste[rpicc]); } } for (int i = 0; i < N; i++) { if (liste[i].size() > maxi) { for (list<int>::iterator it = liste[i].begin(); it != liste[i].end(); it++) for (list<int>::iterator sec = it; sec != liste[i].end(); sec++) if (*it != *sec && nemici[*it].find(*sec) != nemici[*it].end()) goto next; maxi = max(maxi, (int)liste[i].size()); } next:; } cout << maxi; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 2002; int N, M, res, c; int gr[MAXN]; int cnt[MAXN]; bool forb[MAXN]; vector<int> g[MAXN]; void dfs(int nd) { cnt[c]++; gr[nd] = c; for (vector<int>::iterator it = g[nd].begin(); it != g[nd].end(); it++) if (!gr[*it]) dfs(*it); } int main() { scanf(" %d %d", &N, &M); for (int a, b, i = 0; i < M; i++) { scanf(" %d %d", &a, &b); g[a].push_back(b); g[b].push_back(a); } for (int i = 1; i <= N; i++) if (!gr[i]) { c++; dfs(i); } scanf(" %d", &M); for (int a, b, i = 0; i < M; i++) { scanf(" %d %d", &a, &b); if (gr[a] == gr[b]) forb[gr[a]] = 1; } for (int i = 1; i <= c; i++) if (!forb[i]) res = max(res, cnt[i]); printf("%d\n", res); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; vector<long long> tree[2], siz[2]; void ini(long long n) { tree[0].resize(n + 1); tree[1].resize(n + 1); siz[0].resize(n + 1); siz[1].resize(n + 1); for (long long i = 1; i <= n; i++) { tree[0][i] = i; tree[1][i] = i; siz[0][i] = 1; siz[1][i] = 1; } } long long root(long long n, long long f) { while (tree[f][n] != n) { tree[f][n] = tree[f][tree[f][n]]; n = tree[f][n]; } return n; } void uni(long long a, long long b, long long f) { a = root(a, f); b = root(b, f); if (siz[f][a] < siz[f][b]) { siz[f][b] += siz[f][a]; tree[f][a] = tree[f][b]; } else { siz[f][a] += siz[f][b]; tree[f][b] = tree[f][a]; } } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); srand(time(NULL)); ; long long n; cin >> n; ini(n); vector<pair<long long, long long> > a, b; long long k; cin >> k; for (long long i = 0; i < k; i++) { long long t, tt; cin >> t >> tt; a.push_back(make_pair(t, tt)); } cin >> k; for (long long i = 0; i < k; i++) { long long tt, t; cin >> t >> tt; b.push_back(make_pair(t, tt)); } for (long long i = 0; i < a.size(); i++) { if (root(a[i].first, 0) != root(a[i].second, 0)) { uni(a[i].first, a[i].second, 0); } } for (long long i = 0; i < b.size(); i++) { if (root(b[i].second, 0) == root(b[i].first, 0)) { siz[0][root(b[i].second, 0)] = 0; } } long long mx = 0; for (long long i = 1; i <= n; i++) { mx = max(mx, siz[0][root(i, 0)]); } cout << mx << endl; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; long long T, n, m, k, x, y, z, l, r, d, ans, a[N], b[N], c[N], mod = 1e9 + 7; int find(int i) { return (a[i] == i ? i : (a[i] = find(a[i]))); } void solve() { cin >> n >> m; for (long long i = 1; i <= n; i++) a[i] = i; for (long long i = 1; i <= m; i++) cin >> x >> y, a[find(x)] = find(y); cin >> m; for (long long i = 1; i <= m; i++) { cin >> x >> y; if (a[find(x)] == find(y)) b[find(x)] = -1; } for (int i = 1; i <= n; i++) c[find(i)]++; for (int i = 1; i <= n; i++) if (b[find(i)] != -1) ans = max(ans, c[find(i)]); cout << ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); }
9
CPP
#include <bits/stdc++.h> using namespace std; class unionfind { public: vector<int> a, size; public: int comp; public: void setsize(int n) { comp = n; for (int i = 0; i < n; i++) { a.push_back(i); size.push_back(1); } } public: void refresh() { a.clear(); size.clear(); comp = 0; } public: int root(int i) { return (a[i] == i) ? i : (a[i] = root(a[i])); } public: bool find(int n1, int n2) { return (root(n1) == root(n2)); } public: void uni(int n1, int n2) { int r1 = root(n1); int r2 = root(n2); if (r1 == r2) return; if (size[r1] < size[r2]) { a[r1] = r2; size[r2] += size[r1]; } else { a[r2] = r1; size[r1] += size[r2]; } comp--; } }; int dx[] = {-1, 1, 0, 0, 0, 0, 0, 0}; int dy[] = {0, 0, 1, -1, 0, 0, 0, 0}; const int inf = 1000000000; const int seivesize = 10005; const double eps = 0.000000009; int main() { int n; cin >> n; int m1, m2; cin >> m1; unionfind ob; ob.setsize(n); for (int i = 0; i < m1; i++) { int t1, t2; cin >> t1 >> t2; t1--; t2--; ob.uni(t1, t2); } cin >> m2; for (int i = 0; i < m2; i++) { int t1, t2; cin >> t1 >> t2; t1--; t2--; if (ob.find(t1, t2)) { ob.size[ob.root(t1)] = -1; } } int ans = 0; for (int i = 0; i < n; i++) { ans = max(ans, ob.size[ob.root(i)]); } cout << ans << endl; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int n, k, m; vector<pair<int, int> > fr, en; int v, u; int cnt[2100], dead[2100]; class dsu { public: int p[2100]; dsu() { for (int i = 0; i < 2100; i++) { p[i] = i; } } int getset(int v) { if (p[v] == v) return v; return p[v] = getset(p[v]); } void unionsets(int a, int b) { a = getset(a); b = getset(b); if (rand() % 2) { p[a] = b; } else { p[b] = a; } } }; int main() { cin >> n; dsu d; cin >> k; for (int i = 0; i < k; i++) { scanf("%d%d", &v, &u); v--, u--; fr.push_back(make_pair(v, u)); d.unionsets(v, u); } cin >> m; for (int i = 0; i < m; i++) { scanf("%d%d", &v, &u); v--, u--; en.push_back(make_pair(v, u)); if (d.getset(v) == d.getset(u)) { dead[d.getset(v)] = 1; } } for (int i = 0; i < n; i++) { cnt[d.getset(i)]++; } int ans = 0; for (int i = 0; i < n; i++) { if (!dead[i]) ans = max(ans, cnt[i]); } cout << ans; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 2e9 + 3; const int N = 2e3 + 3; const int MOD = 1e9 + 7; const double EPS = 1e-9; const double PI = acos(-1); vector<int> gr[N]; char used[N]; bool enemies[N][N]; vector<int> comp; void dfs(int cur) { used[cur] = 1; for (int v : gr[cur]) { if (!used[v]) dfs(v); } comp.push_back(cur); } int check() { for (int x : comp) { for (int y : comp) { if (x != y && enemies[x][y]) return 0; } } return comp.size(); } void solve() { int n, k, m; cin >> n >> k; for (int i = 0; i < k; i++) { int a, b; cin >> a >> b; gr[a].push_back(b); gr[b].push_back(a); } cin >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; enemies[a][b] = enemies[b][a] = 1; } int ans = 0; for (int v = 1; v <= n; v++) { if (!used[v]) { comp.clear(); dfs(v); ans = max(ans, check()); } } cout << ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); int t; t = 1; while (t--) { solve(); } }
9
CPP
#include <bits/stdc++.h> using namespace std; const int N = 4444; const int M = 444444; int ev[M], nxt[M], head[N]; int dfn[N], low[N], stk[N], id[N], sum[N]; int scc, top, idx, e; bool use[N]; void init() { memset(head, -1, sizeof(head)); memset(dfn, 0, sizeof(dfn)); memset(sum, 0, sizeof(sum)); memset(use, 0, sizeof(use)); scc = top = idx = e = 0; } void addedge(int u, int v) { ev[e] = v; nxt[e] = head[u]; head[u] = e++; } void tarjan(int u) { int i, v; dfn[u] = low[u] = ++idx; use[u] = 1; stk[++top] = u; for (i = head[u]; ~i; i = nxt[i]) { v = ev[i]; if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]); else if (use[v]) low[u] = min(low[u], low[v]); } if (dfn[u] == low[u]) { do { v = stk[top--]; use[v] = 0; id[v] = scc; sum[scc]++; } while (v != u); scc++; } } int main() { int n; cin >> n; int k; cin >> k; init(); for (int i = 0; i < k; i++) { int u, v; scanf("%d%d", &u, &v); addedge(u, v); addedge(v, u); } for (int i = 1; i <= n; i++) if (!dfn[i]) tarjan(i); cin >> k; for (int i = 0; i < k; i++) { int u, v; scanf("%d%d", &u, &v); if (id[u] == id[v]) sum[id[u]] = 0; } int ans = 0; for (int i = 0; i < scc; i++) ans = max(ans, sum[i]); cout << ans << endl; }
9
CPP
#include <bits/stdc++.h> using namespace std; struct node { int x; node *next; }; int visit[2005]; node *li[100005]; int ans[2005]; node *new_node() { static node t[100005]; static int top = 0; t[top].x = 0; t[top].next = 0; return &t[top++]; } int insertedge(int x, int y) { node *t = new_node(); t->x = y; t->next = li[x]; li[x] = t; node *t2 = new_node(); t2->x = x; t2->next = li[y]; li[y] = t2; return 0; } int dfs(int now, int temp) { if (visit[now] != 0) return 0; visit[now] = temp; node *x; int sum = 0; for (x = li[now]; x != 0; x = x->next) { sum += dfs(x->x, temp); } return sum + 1; } int main() { memset(li, 0, sizeof(li)); int n; int i; int k, m; int x, y; scanf("%d", &n); scanf("%d", &k); for (i = 1; i <= k; i++) { scanf("%d%d", &x, &y); insertedge(x, y); } scanf("%d", &m); int ma = 0; memset(visit, 0, sizeof(visit)); for (i = 1; i <= n; i++) { ans[i] = dfs(i, i); } for (i = 1; i <= m; i++) { scanf("%d%d", &x, &y); if (visit[x] == visit[y]) ans[visit[x]] = 0; } for (i = 1; i <= n; i++) { ma = max(ma, ans[i]); } printf("%d\n", ma); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int n, m, k; bool flag[2003]; int pred[2003]; int size[2003]; int findset(int v) { if (v == pred[v]) return v; return pred[v] = findset(pred[v]); } void makeset(int v) { size[v] = 1; pred[v] = v; } void uniset(int a, int b) { int x = findset(a); int y = findset(b); if (x != y) { if (size[x] >= size[y]) { size[x] += size[y]; pred[y] = x; } else { size[y] += size[x]; pred[x] = y; } } } int main() { scanf("%d", &n); scanf("%d", &k); int i; int x, y; for (i = 0; i < n; i++) makeset(i); for (i = 0; i < k; i++) { scanf("%d %d", &x, &y); x--; y--; uniset(x, y); } scanf("%d", &m); for (i = 0; i < m; i++) { scanf("%d %d", &x, &y); x--; y--; x = findset(x); y = findset(y); if (x == y) flag[x] = true; } int ans = 0; for (i = 0; i < n; i++) { x = findset(i); if (!flag[x]) ans = max(ans, size[x]); } printf("%d\n", ans); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int arr[100001]; int siz[100001]; int root(int i) { while (arr[i] != i) { arr[i] = arr[arr[i]]; i = arr[i]; } return i; } void we_uni(int a, int b) { int ra = root(a); int rb = root(b); if (siz[ra] < siz[rb]) { arr[ra] = rb; siz[rb] += siz[ra]; } else { arr[rb] = ra; siz[ra] += siz[rb]; } } bool fin(int a, int b) { if (root(a) == root(b)) return 1; else return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { arr[i] = i; siz[i] = 1; } set<int> s; for (int i = 1; i <= n; i++) s.insert(i); while (k--) { int x, y; cin >> x >> y; if (fin(x, y)) continue; s.erase(root(x)); s.erase(root(y)); we_uni(x, y); s.insert(root(x)); } int m; cin >> m; while (m--) { int x, y; cin >> x >> y; if (fin(x, y)) s.erase(root(x)); } set<int>::iterator itr; int maxi = 0; for (itr = s.begin(); itr != s.end(); ++itr) maxi = max(maxi, siz[*itr]); cout << maxi; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; class vertex { public: bool mark; int comp; vector<pair<int, bool>> to; }; int n, k, m; vertex f[2000]; int sz[2000]; int counter = 1; int maxInv = 0; bool checkComponentX(int c) { bool good = true; for_each(f + 1, f + n + 1, [&good, c](vertex v) mutable { bool x = true; if (v.comp == c) { for_each(v.to.begin(), v.to.end(), [&x, c](pair<int, bool> w) mutable { x = f[w.first].comp == c && w.second == 0 ? 0 : x; }); } if (x == false) good = false; }); return good; } bool checkComponent(int c) { for (int i = 1; i <= n; i++) { if (f[i].comp == c) { for (int j = 0; j < f[i].to.size(); j++) { if (f[f[i].to[j].first].comp == c && f[i].to[j].second == false) return false; } } } return true; } void dfs(int v) { f[v].mark = true; f[v].comp = counter; sz[counter]++; for (auto w : f[v].to) { if (w.second == 1 && f[w.first].mark == false) { dfs(w.first); } } } int main() { ios::sync_with_stdio(0); cin >> n >> k; int a, b; for (int i = 0; i <= k - 1; i++) { cin >> a >> b; f[a].to.push_back({b, 1}); f[b].to.push_back({a, 1}); } cin >> m; for (int i = 0; i <= m - 1; i++) { cin >> a >> b; f[a].to.push_back({b, 0}); f[b].to.push_back({a, 0}); } for (int i = 1; i <= n; i++) { if (f[i].mark == false) { dfs(i); counter++; } } int compN = counter - 1; for (int i = 1; i <= compN; i++) { if (checkComponentX(i)) { if (sz[i] > maxInv) maxInv = sz[i]; } } cout << maxInv; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int N = 20202; vector<int> adj[N]; bool vis[N]; vector<int> component; int n, k, m, ans; set<pair<int, int> > noFriend; void dfs(int node) { vis[node] = 1; component.push_back(node); for (auto v : adj[node]) { if (!vis[v]) dfs(v); } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> k; for (int i = 1; i <= k; i++) { int x, y; cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } memset(vis, 0, sizeof vis); cin >> m; for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; noFriend.insert({x, y}); noFriend.insert({y, x}); } for (int k = 1; k <= n; k++) { component.clear(); if (!vis[k]) dfs(k); bool f = 1; for (int i = 0; i < component.size(); i++) { for (int j = i + 1; j < component.size(); j++) { pair<int, int> t = {component[i], component[j]}; if (noFriend.count(t) > 0) { f = false; break; } } } if (f && component.size() >= ans) { ans = component.size(); } } cout << ans << endl; }
9
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 2e3 + 10, big = 1e18, mod = 1e9 + 7; vector<long long> a[N], now, ans; long long n, x, y, m, maxi; bool vis[N], mp[N][N]; void dfs(long long i) { vis[i] = 1, now.push_back(i); for (int j = 0; j < a[i].size(); j++) if (!vis[a[i][j]]) dfs(a[i][j]); } int main() { cin >> n; cin >> m; while (m--) { cin >> x >> y; a[x].push_back(y); a[y].push_back(x); } cin >> m; while (m--) { cin >> x >> y; mp[x][y] = mp[y][x] = 1; } for (int i = 1; i <= n; i++) if (!vis[i]) { now.clear(); dfs(i); bool check = 1; if (now.size() == 1) maxi = max(1LL, maxi); else { for (int j = 0; j < now.size() - 1; j++) for (int k = j + 1; k < now.size(); k++) if (mp[now[j]][now[k]]) check = 0; if (check) maxi = max((long long)now.size(), maxi); } } cout << maxi; }
9
CPP
#include <bits/stdc++.h> using namespace std; int n, k, m; vector<int> fr[100010]; vector<int> dis[100010]; bool vis[2010]; bool red[2010]; vector<int> V; vector<int> ans; void bfs(int in) { V.resize(0); memset(vis, false, sizeof vis); memset(red, false, sizeof red); V.push_back(in); vis[in] = true; int cnt = 0; for (int i = 0; i < (int)dis[in].size(); i++) red[dis[in][i]] = true; while (1) { while (cnt < (int)V.size()) { int cur = V[cnt]; if (red[cur]) return; for (int i = 0; i < (int)dis[cur].size(); i++) red[dis[cur][i]] = true; for (int i = 0; i < (int)fr[cur].size(); i++) { int moj = fr[cur][i]; if (vis[moj] == false) { vis[moj] = true; V.push_back(moj); } } cnt++; } ans.push_back((int)V.size()); for (int i = 1; i <= n; i++) if (vis[i] == false && red[i] == false) { for (int j = 0; j < (int)dis[i].size(); j++) red[dis[i][j]] = true; V.push_back(i); continue; } return; } } int main() { cin >> n; cin >> k; for (int i = 0; i < k; i++) { int u, v; cin >> u >> v; fr[u].push_back(v); fr[v].push_back(u); } cin >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; dis[u].push_back(v); dis[v].push_back(u); } for (int i = 1; i <= n; i++) bfs(i); int ANS = 0; for (int i = 0; i < (int)ans.size(); i++) ANS = max(ans[i], ANS); cout << ANS << endl; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5; static int comp[maxn]; static int pr[maxn]; static int n; void make_set() { for (int i = 0; i < n; ++i) { pr[i] = i; comp[i] = 1; } } int find_set(int a) { if (pr[a] == a) return a; return pr[a] = find_set(pr[a]); } void union_set(int a, int b) { a = find_set(a); b = find_set(b); pr[a] = b; comp[b] += comp[a]; comp[a] = 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n; int m; cin >> m; make_set(); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; a--; b--; if (find_set(a) != find_set(b)) union_set(a, b); } int k; cin >> k; for (int j = 0; j < k; ++j) { int a, b; cin >> a >> b; a--; b--; if (find_set(a) == find_set(b)) { comp[find_set(a)] = 0; } } int mx = 0; for (int l = 0; l < n; ++l) { mx = max(mx, comp[l]); } cout << mx; }
9
CPP
#include <bits/stdc++.h> using namespace std; int g[2001][2001], n, k = 0, a[2001], s[2001] = {0}, m; bool viz[2001] = {0}; set<int> mys; void dfs(int x) { viz[x] = 1; a[x] = k; s[k]++; for (int i = 1; i <= n; i++) if ((g[x][i] == 1) && (!viz[i])) dfs(i); }; int main() { cin >> n >> m; for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; g[x][y] = 1; g[y][x] = 1; }; for (int i = 1; i <= n; i++) if (viz[i] == 0) { k++; dfs(i); }; cin >> m; for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; if (a[x] == a[y]) if (mys.find(a[x]) == mys.end()) mys.insert(a[x]); }; int max = 0; for (int i = 1; i <= k; i++) { if ((s[i] > max) && (mys.find(i) == mys.end())) max = s[i]; }; cout << max; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int dsu[2001]; int size[2001]; void assign(int n) { for (int i = 0; i <= n; i++) { dsu[i] = i; size[i] = 1; } } int find(int i) { while (i != dsu[i]) { dsu[i] = dsu[dsu[i]]; i = dsu[i]; } return i; } void unionn(int x, int y) { int xx = find(x); int yy = find(y); if (size[xx] <= size[yy]) { size[yy] += size[xx]; dsu[xx] = yy; } else { size[xx] += size[yy]; dsu[yy] = xx; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; assign(n); int k; cin >> k; int x, y; for (int i = 0; i < int(k); i++) { cin >> x >> y; unionn(x, y); } int m; cin >> m; for (int i = 0; i < int(m); i++) { cin >> x >> y; if (find(x) == find(y)) { unionn(0, x); } } int NOO = find(0); map<int, int> count; for (int i = 0; i < int(n + 1); i++) { if (find(i) != NOO) { count[find(i)]++; } } int MAX = 0; for (auto it : count) { if (it.second > MAX) MAX = it.second; } cout << MAX << "\n"; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int m, n, t, q, r, w, a, b; int p[3000], cnt[3000] = {0}; bool ok[3000]; int find(int x) { return x == p[x] ? x : (p[x] = find(p[x])); } void uni(int x, int y) { p[find(x)] = find(y); } bool eqi(int x, int y) { return find(x) == find(y); } int main() { for (int i = 0; i < 3000; i++) { ok[i] = 1; p[i] = i; } scanf("%d", &t); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d%d", &a, &b); uni(a, b); } scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d%d", &a, &b); if (eqi(a, b)) { ok[find(a)] = 0; } } for (int i = 1; i <= t; i++) { if (ok[find(i)]) cnt[find(i)]++; } int ret = 0; for (int i = 1; i <= t; i++) if (cnt[i] > ret) ret = cnt[i]; printf("%d\n", ret); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = (int)((1e5) + 10); int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } int max(int a, int b) { if (a > b) return a; else return b; } int min(int a, int b) { if (a < b) return a; else return b; } bool isPrime(int N) { for (int i = 2; i * i <= N; ++i) { if (N % i == 0) return false; } return true; } int cbrt(int x) { int lo = 1, hi = min(2000000ll, x); while (hi - lo > 1) { int mid = (lo + hi) / 2; if (mid * mid * mid < x) { lo = mid; } else hi = mid; } if (hi * hi * hi <= x) return hi; else return lo; } const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; const int nax = (int)(100000 + 10); int n, t, a, b, m, m2, ans = 0, s = 0; vector<int> adj[200002], em[200002]; int vis[200002]; void dfs(int u) { vis[u] = 1; s += 1; for (auto &i : adj[u]) { if (!vis[i]) { dfs(i); } } for (auto &i : em[u]) { if (vis[i]) { s = -1e9; } } } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } cin >> m2; for (int i = 0; i < m2; i++) { cin >> a >> b; em[a].push_back(b); em[b].push_back(a); } for (int i = 1; i <= n; i++) { s = 0; dfs(i); ans = max(ans, s); memset(vis, 0, sizeof vis); } cout << ans << "\n"; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2010; struct NodeDSU { int rank, parent; } dsu[N]; int n, k, m, c, p, sol; set<int> skup[N]; vector<int> hate[N]; map<int, int> hes; void MakeSet(int n) { for (int i = 1; i <= n; i++) { dsu[i].rank = 0; dsu[i].parent = i; } } int FindSet(int x) { if (dsu[x].parent == x) return x; dsu[x].parent = FindSet(dsu[x].parent); return dsu[x].parent; } void UniteSet(int x, int y) { x = FindSet(x); y = FindSet(y); if (x == y) return; if (dsu[x].rank < dsu[y].rank) dsu[x].parent = y; else dsu[y].parent = x; if (dsu[x].rank == dsu[y].rank) dsu[x].rank++; } int main() { scanf("%d%d", &n, &m); MakeSet(n); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); UniteSet(a, b); } scanf("%d", &k); for (int i = 0; i < k; i++) { int a, b; scanf("%d%d", &a, &b); hate[a].push_back(b); hate[b].push_back(a); } for (int i = 1; i <= n; i++) { p = FindSet(i); if (hes.count(p) == 0) hes[p] = c++; skup[hes[p]].insert(i); } for (int i = 0; i < c; i++) { bool ok = true; for (auto it : skup[i]) { int v = it; for (int j = 0; j < hate[v].size(); j++) { int u = hate[v][j]; if (skup[i].find(u) != skup[i].end()) ok = false; } } if (ok) sol = max(sol, (int)(skup[i].size())); } cout << sol; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; long long id[100005]; long long sz[100005]; void initialize() { for (long long i = 1; i <= 100000; i++) { id[i] = i; sz[i] = 1; } } long long root(long long x) { while (x != id[x]) { id[x] = id[id[x]]; x = id[x]; } return x; } void union_(long long x, long long y) { long long a = root(x); long long b = root(y); if (a != b) { if (sz[a] <= sz[b]) { id[a] = b; sz[b] += sz[a]; } else { id[b] = a; sz[a] += sz[b]; } } } int main() { initialize(); long long n; cin >> n; long long k; cin >> k; for (long long i = 0; i < k; i++) { long long x, y; cin >> x >> y; if (root(x) != root(y)) union_(x, y); } long long m; cin >> m; for (long long i = 0; i < m; i++) { long long x, y; cin >> x >> y; if (root(x) == root(y)) id[root(x)] = 0; } long long ma = 0; for (long long i = 1; i <= n; i++) { if (id[i] == i) ma = max(ma, sz[i]); } cout << ma; }
9
CPP
#include <bits/stdc++.h> using namespace std; void bfs(vector<vector<int> >& G, vector<int>& components, vector<int>& sizes, int vertex_s, int number_of_component) { queue<int> Queue; Queue.push(vertex_s); components[vertex_s] = number_of_component; int current_vertex; int cur_size = 1; while (!Queue.empty()) { current_vertex = Queue.front(); Queue.pop(); for (int i = 0; i < G.size(); i++) { if ((G[current_vertex][i] == 1) && (components[i] == -1)) { components[i] = number_of_component; cur_size++; Queue.push(i); }; }; }; sizes.push_back(cur_size); } int main() { int N, K, M; cin >> N >> K; vector<vector<int> > love(N, vector<int>(N, 0)); vector<vector<int> > hate(N, vector<int>(N, 0)); for (int i = 0; i < K; i++) { int a, b; cin >> a >> b; love[a - 1][b - 1] = love[b - 1][a - 1] = 1; } cin >> M; for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; hate[a - 1][b - 1] = hate[b - 1][a - 1] = 1; } vector<int> components(N, -1); vector<int> sizes; int cur_ver = 0; int i = -1; while (cur_ver < N) { i++; bfs(love, components, sizes, cur_ver, i); while ((cur_ver < N) && (components[cur_ver] != -1)) { cur_ver++; } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if ((components[i] == components[j]) && (hate[i][j] == 1)) sizes[components[i]] = 0; } } int max = 0; for (int i = 0; i < sizes.size(); i++) { if (max < sizes[i]) max = sizes[i]; } cout << max << endl; return 0; }
9
CPP
# maa chudaaye duniya n = int(input()) parents = [i for i in range(n+1)] ranks = [1 for i in range(n+1)] def find(x): if parents[x] != x: parents[x] = find(parents[x]) return parents[x] def union(x, y): xs = find(x) ys = find(y) if xs == ys: return if ranks[xs] > ranks[ys]: parents[ys] = xs elif ranks[ys] > ranks[xs]: parents[xs] = ys else: parents[ys] = xs ranks[xs] += 1 for _ in range(int(input())): u, v = map(int, input().split()) union(u, v) # print(parents) rejects = set([]) for _ in range(int(input())): p, q = map(int, input().split()) ps = find(p) qs = find(q) if ps == qs: rejects.add(ps) ps = {} for i in range(1, n+1): p = find(i) if p not in rejects: if p in ps: ps[p] += 1 else: ps[p] = 1 # print(ps) ans = 0 for i in ps: ans = max(ans, ps[i]) print(ans)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; long long int n, m, grp, p, mat[2005][2005], ans; bool mark[2005]; vector<long long int> adj[2005], comp[2005]; void dfs(long long int v) { mark[v] = true; comp[grp].push_back(v); for (long long int j = 0; j < adj[v].size(); j++) { if (!mark[adj[v][j]]) dfs(adj[v][j]); } } bool check(long long int x) { for (long long int i = 0; i < comp[x].size() - 1; i++) { for (long long int j = i + 1; j < comp[x].size(); j++) { if (mat[comp[x][i]][comp[x][j]] == 1) return false; } } return true; } int main() { long long int i, x, y; cin >> n >> m; for (i = 0; i < m; i++) { cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } cin >> p; for (i = 0; i < p; i++) { cin >> x >> y; mat[x][y] = 1; mat[y][x] = 1; } for (i = 1; i <= n; i++) { if (!mark[i]) { dfs(i); grp++; } } for (i = 0; i < grp; i++) { if (check(i)) { ans = ans > comp[i].size() ? ans : comp[i].size(); } } cout << ans << endl; }
9
CPP
#include <bits/stdc++.h> using namespace std; int main() { unsigned int n, k, m, ans = 0; scanf("%d", &n); vector<vector<int> > like(n + 1); vector<set<int> > hate(n + 1); scanf("%d", &k); for (unsigned int(i) = 0; (i) < (k); ++(i)) { int x, y; scanf("%d %d", &x, &y); like[x].push_back(y); like[y].push_back(x); } scanf("%d", &m); for (unsigned int(i) = 0; (i) < (m); ++(i)) { int x, y; scanf("%d %d", &x, &y); hate[x].insert(y); hate[y].insert(x); } vector<bool> used(n + 1); for (unsigned int(i) = 0; (i) < (n); ++(i)) { int first_p = i + 1; if (!used[first_p]) { used[first_p] = true; bool HateFlag = false; vector<int> f_group; f_group.push_back(first_p); for (unsigned int(j) = 0; (j) < (f_group.size()); ++(j)) { int p1 = f_group[j]; for (unsigned int(q) = 0; (q) < (like[p1].size()); ++(q)) { int p2 = like[p1][q]; if (!used[p2]) { used[p2] = true; for (unsigned int(r) = 0; (r) < (f_group.size()); ++(r)) { if (hate[f_group[r]].count(p2) > 0) { HateFlag = true; } } f_group.push_back(p2); } } } if (!HateFlag) { if (f_group.size() > ans) { ans = f_group.size(); } } } } printf("%d", ans); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; void dfs(long long &m, vector<bool> &dist, unordered_map<long long, vector<long long>> &adjlist, unordered_map<long long, vector<long long>> &dislike, vector<bool> &temp, long long y) { for (auto i : adjlist[y]) { if (!dist[i]) { dist[i] = true; temp[i] = true; m++; dfs(m, dist, adjlist, dislike, temp, i); } } for (auto i : dislike[y]) { if (temp[i]) { m = -1e9; break; } } } int main() { long long n, m; cin >> n >> m; unordered_map<long long, vector<long long>> adjlist, dislike; for (long long i = 0; i < m; i++) { long long x, y; cin >> x >> y; adjlist[x].push_back(y); adjlist[y].push_back(x); } long long p; cin >> p; for (long long i = 0; i < p; i++) { long long x, y; cin >> x >> y; dislike[x].push_back(y); dislike[y].push_back(x); } long long ans(0), s(0); vector<bool> dist(n + 1, false); for (long long i = 1; i <= n; i++) { if (!dist[i]) { vector<bool> temp(n + 1, false); s = 1; dist[i] = true; dfs(s, dist, adjlist, dislike, temp, i); ans = max(ans, s); } } cout << ans << endl; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int get_root(int P[], int u) { while (u != P[u]) { P[u] = P[P[u]]; u = P[u]; } return u; } void union_sets(int P[], int S[], int u, int v) { u = get_root(P, u); v = get_root(P, v); if (u != v) { if (S[u] >= S[v]) { P[v] = u; S[u] += S[v]; } else { P[u] = v; S[v] += S[u]; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k, m, u, v; cin >> n; int P[n + 1], S[n + 1]; for (int i = 1; i <= n; i++) { P[i] = i; S[i] = 1; } cin >> k; for (int i = 1; i <= k; i++) { cin >> u >> v; union_sets(P, S, u, v); } cin >> m; map<int, bool> F; map<int, vector<int>> C; for (int i = 1; i <= n; i++) { F[get_root(P, i)] = true; C[get_root(P, i)].push_back(i); } for (int i = 1; i <= m; i++) { cin >> u >> v; if (get_root(P, u) == get_root(P, v)) { F[get_root(P, u)] = false; } } int maxf = 0; for (auto f : F) { if (f.second) { if ((int)C[f.first].size() > maxf) maxf = C[f.first].size(); } } cout << maxf << endl; return 0; }
9
CPP
def dfs (chain): if chain: for c in chain: if c not in groups: groups.add(c) dfs(graph[c-1]) graph[c-1].clear() graph= [[] for _ in range(int(input()))]; sev = len(graph) groups,inv,ind = set(),{},1 for _ in range(int(input())): u,v = map(int,input().split()) graph[u-1].append(v) for i,g in enumerate(graph): if g: groups.add(i+1) dfs(g); inv[ind] = [p for p in groups] ind+=1; groups.clear() graph = [set() for _ in range(sev)] for f in inv: for k in inv[f]: graph[k-1].add(f) for g in range(sev): if not graph[g]: inv[g+1] = [g+1] for _ in range(int(input())): cat = [x for x in map(int,input().split())] gat = graph[cat[0]-1].intersection(graph[cat[1]-1]) if gat: for s in cat: for l in graph[s-1]: try: inv.pop(l) except KeyError: continue if inv: print(len(max(inv.items(),key = lambda x: len(x[1]))[1])) else: print(0)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; long long int mod = 1e9 + 7; long long int const maxn = 2e3 + 5; long long int const inf = 1e18; long long int add(long long int a, long long int b) { return ((a % mod) + (b % mod)) % mod; } long long int mul(long long int a, long long int b) { return ((a % mod) * (b % mod)) % mod; } long long int powm(long long int x, long long int n, long long int M) { long long int result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % M; x = (x * x) % M; n = n / 2; } return result; } bool prime(long long int x) { if (x < 2) return false; for (int i = 2; i <= sqrt(x); i++) { if (x % i == 0) return false; } return true; } long long int P[maxn]; long long int sz[maxn]; long long int find_set(long long int x) { if (x != P[x]) P[x] = find_set(P[x]); return P[x]; } void merge_set(long long int a, long long int b) { a = find_set(a); b = find_set(b); if (a != b) { if (sz[b] >= sz[a]) swap(a, b); P[b] = a; sz[a] += sz[b]; sz[b] = 0; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); for (int i = 0; i <= 2000; i++) { P[i] = i; sz[i] = 1; } long long int n; cin >> n; long long int k; cin >> k; while (k--) { long long int x, y; cin >> x >> y; if (find_set(x) != find_set(y)) { merge_set(x, y); } } long long int m; cin >> m; while (m--) { long long int x, y; cin >> x >> y; x = find_set(x); y = find_set(y); if (x == y) { sz[x] = 0; sz[y] = 0; } } long long int ans = 0; for (int i = 1; i <= n; i++) ans = max(ans, sz[P[i]]); cout << ans << "\n"; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; long long int n, k, m; map<pair<long long int, long long int>, long long int> mp; vector<vector<long long int>> v(2005); vector<long long int> vis; vector<long long int> s; void dfs(long long int r) { vis[r] = 1; s.push_back(r); for (auto cc : v[r]) { if (vis[cc] == 0) { dfs(cc); } } } int32_t main() { cin >> n; vis.resize(n + 1, 0); v.resize(n + 1); long long int mx = 0; cin >> k; for (long long int i = 0; i < k; i++) { long long int x, y; cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } cin >> m; for (long long int i = 0; i < m; i++) { long long int x, y; cin >> x >> y; mp[make_pair(x, y)]++; mp[make_pair(y, x)]++; } vector<vector<long long int>> v; for (long long int i = 1; i <= n; i++) { if (vis[i] == 0) { vector<long long int> t; dfs(i); v.push_back(s); s = t; } } for (long long int i = 0; i < v.size(); i++) { long long int in = 0; for (long long int j = 0; j < v[i].size() - 1; j++) { for (long long int jj = j + 1; jj < v[i].size(); jj++) { if (mp[{v[i][j], v[i][jj]}] > 0 || mp[{v[i][jj], v[i][j]}] > 0) in++; } } long long int xx = v[i].size(); if (in == 0) mx = max(mx, xx); } cout << mx << endl; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 3e4 + 100; int root[MAXN], sz[MAXN]; bool mark[MAXN]; int get_root(int u) { return (root[u] ? root[u] = get_root(root[u]) : u); } void merge(int u, int v) { int ru = get_root(u); int rv = get_root(v); if (rv == ru) return; root[ru] = rv; sz[rv] += sz[ru]; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k, m, x, y; cin >> n >> k; fill(sz + 1, sz + n + 1, 1); for (int i = 0; i < k; i++) { cin >> x >> y; merge(x, y); } cin >> m; for (int i = 0; i < m; i++) { cin >> x >> y; if (get_root(x) == get_root(y)) mark[get_root(y)] = true; } int ans = 0; for (int i = 1; i <= n; i++) { int r = get_root(i); if (mark[r]) continue; ans = max(ans, sz[r]); mark[r] = true; } cout << ans << endl; return 0; }
9
CPP