prompt
string
response
string
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long n, m; cin >> n >> m; vector<long long> B(n), G(m); for (long long &x : B) cin >> x; for (long long &x : G) cin >> x; sort(B.begin(), B.end()); sort(G.begin(), G.e...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
#include <bits/stdc++.h> using namespace std; const int N = 100100; int n, m; int b[N], g[N]; int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", b + i); for (int i = 1; i <= m; ++i) scanf("%d", g + i); sort(b + 1, b + 1 + n); sort(g + 1, g + 1 + m); if (b[n] > g[1]) { puts("-1...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
import bisect import decimal from decimal import Decimal import os from collections import Counter import bisect from collections import defaultdict import math import random import heapq from math import sqrt import sys from functools import reduce, cmp_to_key from collections import deque import threading from itert...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
def getn(): return int(input()) def getns(): return [int(x) for x in input().split()] n,m=getns() ns=getns() ms=getns() if max(ns)>min(ms): print(-1) quit() ans=sum(ns)*m if max(ns)==min(ms): ans+=sum(ms)-max(ns)*m print(ans) quit() ns.sort() ans+=sum(ms)-max(ns)*m ans+=ns[-1]-ns[-2] print(a...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
#include <bits/stdc++.h> using namespace std; long long n, m, k, l, r, c = 0, d, sum = 0, a, b, x, y, z, v, s, c1, c2, id, mx = -1, flg = 0; long long B[2000001 + 5], G[2000001 + 5]; vector<long long> V; set<long long> S; map<long long, long long> Mp; priority_queue<long long> Pq; char str[500]...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
#include <bits/stdc++.h> using namespace std; long long b[100005]; long long g[100005]; int main() { long long n, m; cin >> n >> m; long long ans = 0; for (long long i = 0; i < n; i++) { cin >> b[i]; ans += m * b[i]; } sort(b, b + n); for (long long i = 0; i < m; i++) { cin >> g[i]; } sort...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
import java.util.*; public class Solution { public static void main(String []args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int b[] = new int[n]; int g[] = new int[m]; for(int i =...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
def main(): n,m = map(int,input().split()) boys = list(map(int,input().split())) girls = list(map(int,input().split())) if min(girls) < max(boys): print(-1) return sweets = 0 boys.sort(reverse = True) girls.sort(reverse = True) if min(girls) == max(boys): swee...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class C { public static void main(String[] args) { FastReader sc = new FastReader(); int boys = sc.nextInt(); int girls = sc.nextInt(); long...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] bs = new int[n]; int[] gs = new int[m]; int maxBoy = -1; long minGive = 0; long budg...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
#include <bits/stdc++.h> using namespace std; long long b[100010], g[100010], n, m; int main() { cin >> n >> m; for (int i = 0; i < n; i++) cin >> b[i]; sort(b, b + n); for (int i = 0; i < m; i++) cin >> g[i]; sort(g, g + m); if (g[0] < b[n - 1]) { cout << -1 << endl; } else if (g[0] == b[n - 1]) { ...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
n, m = list(map(int, input().split())) b = list(map(int, input().split())) g = list(map(int, input().split())) b.sort() g.sort() if b[-1] > g[0]: print(-1) elif b[-1] == g[0]: print(sum(g) + m * (sum(b) - b[-1])) elif n == 1: print(-1) else : print(sum(g) + b[-1] + b[-2] * (m - 1) + m * (sum(b) - b[-1] ...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
#include <bits/stdc++.h> using namespace std; void solve() { long long int n, m, i, j; cin >> n >> m; long long int boy[n], girl[m]; long long int mm = 0; long long int ck = 0; for (i = 0; i < n; i++) { cin >> boy[i]; mm = max(mm, boy[i]); ck += (boy[i] * m); } long long int mi = 1e9; for ...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
#include <bits/stdc++.h> using namespace std; const int N = 400000 + 5; long long maxx = 0, maxx2 = 0, minn = (int)1e9; long long sumboy = 0, sumgirl = 0; long long x; long long ans = 0; int n, m; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = (1); i <= (n); i++) { ...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
#include <bits/stdc++.h> using namespace std; long long int expo_fast(long long int a, long long int b) { a = a; long long int result = 1; while (b) { if (b & 1) result = (result * a); b >>= 1; a = (a * a); } return (result); } void take_in(vector<long long int>* arr) { for (int i = 0; i < arr->...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
n,m=map(int,raw_input().split()) a=list(map(int,raw_input().split())) b=list(map(int,raw_input().split())) a.sort(),b.sort() if a[-1]>b[0]: print -1 elif a[-1] in b: print sum(a[:-1])*m+sum(b) else: print sum(a[:-1])*m+sum(b)+(a[-1]-a[-2])
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(20); int n, m; cin >> n >> m; long long int a[n], b[m], mx, mn = INT_MAX, s1 = 0, s2 = 0; for (int i = 0; i < n; i++) { cin >> a[i]; s1 += a[i]; } sort(a, a + n); mx = a[n ...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
import java.util.*; import java.math.*; import java.io.*; public class A{ static FastReader scan=new FastReader(); public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out)); static LinkedList<Integer>edges[]; static boolean stdin = true; static String filein = "input"; sta...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
import java.util.*; import java.io.*; import java.math.BigInteger; public class C{ public static long mod = 1000000007; public static int dx[] = {-1,0,1,0,-1,-1,1,1,-1};//primero ady, despues esq public static int dy[] = {0,1,0,-1,1,1,-1,-1}; public static void main(String args[]) throws IOException{ StringBuilder sb =...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
import java.io.*; import java.util.*; public class Main { //static ArrayList a[]=new ArrayList[3000001]; public static void main(String[]args) throws IOException { InputReader in=new InputReader(System.in); PrintWriter pw=new PrintWriter(System.out); int n=in.nextInt(); i...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
import java.util.*; import java.io.*; public class A { public static void main(String ar[]) throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String s1[]=br.readLine().split(" "); String s2[]=br.readLine().split(" "); S...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m, i, j, sw = 0, nsw; bool presMin; cin >> n >> m; vector<long long> b(n), g(m); for (i = 0; i < n; i++) cin >> b[i]; for (i = 0; i < m; i++) cin >> g[i]; sort(b.rbegin(), b.rend...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
n, m = map(int, input().split()) b = list(map(int, input().split())) g = list(map(int, input().split())) Sum = 0 for j in range(n): Sum+=b[j]*m b.sort() for i in range(m): Sum+=max(0, g[i]-b[-1]) if min(g)<max(b): print(-1) elif min(g)==max(b): print(Sum) else: Sum+=b[-1]-b[-2]; print(Sum)
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
import java.io.*; import java.util.*; import java.util.function.Function; public class MainC { static int N, M; static long[] B; static Long[] G; public static void main(String[] args) { FastScanner sc = new FastScanner(System.in); N = sc.nextInt(); M = sc.nextInt(); B...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
n,m=map(int,input().split()) b=[int(x) for x in input().split()] g=[int(x) for x in input().split()] b1=max(b) g1=min(g) c=b[:] c.remove(b1) max2=max(c) if b1>g1: print(-1) elif b1==g1: print((sum(b)*m)+sum(g)-(b1*m)) else: #print(56) print((sum(b)*m)-(b1*m)+sum(g)+b1-max2)
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
import java.io.*; import java.util.*; import java.lang.*; public class A { public static void main(String[] args) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(), m = in.nextInt(); int[] a = new int[n]; int[] b = new int[m]; int maxB = -1; int min...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
import java.util.Arrays; import java.util.Scanner; public class PartyAndSweets { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); int M = in.nextInt(); int[] boys = new int[N]; int[] girls = new int[M]; long sum = 0;...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
#include <bits/stdc++.h> using namespace std; int main() { long long int n, m, ans = 0; cin >> n >> m; long long int b[n], g[m]; for (int i = 0; i < n; i++) { cin >> b[i]; } for (int i = 0; i < m; i++) { cin >> g[i]; } sort(b, b + n, greater<int>()); sort(g, g + m, greater<int>()); if (g[m -...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
import java.util.Arrays; import java.util.Scanner; public class C { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] bs = new int[n]; int[] gs = new int[m]; int maxBoy = -1; lon...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<long long> b(n), g(m); long long ans = 0; long long gs = 0; for (int i = 0; i < n; i++) { cin >> b[i]; ans += b[i] * m; } for (int i = 0; i < m; i++) { cin >> g[i]; gs += g[i]; } sort(b.begin()...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
R = lambda: map(int, raw_input().split()) n, m = R() b = R() g = R() mxb = max(b) mig = min(g) if mxb > mig: print -1 else: b.sort(reverse=1) s = sum(b) * m + sum(g) - b[0] * m if mxb < mig: s += b[0] - b[1] print s
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
import bisect n,m = map(int,raw_input().split()) b = map(int,raw_input().split()) g = map(int,raw_input().split()) ans = m*sum(b) b.sort() g.sort(None,None,True) imp = 0 for j in g: if j<b[n-1]: imp = 1 break if imp : print -1 else: x = n-1 cnt = m-1 for i in range(m): # print g[i],b[x] if g[i] == b[x]: ...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
#include <bits/stdc++.h> const int N = 2e6, inf = 1e9 + 7; using namespace std; int n, m, a[N], b[N]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i <= m; ++i) scanf("%d", &b[i]); sort(a + 1, a + n + 1); sort(b + 1, b + m + 1); if (a[n] > b[1]) { p...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
/* * * @Author Ajudiya_13(Bhargav Girdharbhai Ajudiya) * Dhirubhai Ambani Institute of Information And Communication Technology * */ import java.util.*; import java.io.*; import java.lang.*; public class Code6 { public static void main(String[] args) { InputReader in = new InputReader(System.in...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, M; cin >> N >> M; vector<long long> boys(N), girls(M); for (auto &b : boys) cin >> b; for (auto &g : girls) cin >> g; sort(boys.begin(), boys.end()); sort(girls.begin(), girls.end()); if ...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
from collections import defaultdict def solve(): if b[-1] > g[0]: return -1 if b[-1] in g: ans = sum(g) + m * b[-2] + m * sum(b[:-2]) else: ans = sum(g) + b[-1] + (m - 1) * b[-2] + m * sum(b[:-2]) occ_b = defaultdict(int) for x in b[:-1]: occ_b[x] += 1 alread...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
import java.util.*; import java.io.*; import java.math.*; /** * * @author alanl */ public class Code { /** * @param args the command line arguments */ static BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter pr = new ...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
import java.io.*; import java.util.*; public class C { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st ...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
#include <bits/stdc++.h> using namespace std; long long mod(long long a) { long long c = a % (long long)(1E9 + 7); if (c >= 0) return c; return c + (long long)(1E9 + 7); } long long inf = (long long)1E17; long long max(long long a, long long b) { if (a < b) return b; else return a; } long long min(lon...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; /** * # * * @author pttrung */ public class C_Rou...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
import java.io.*; import java.util.*; public class C { public static void main(String[] args) { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); Solver solver = new Solver(); solver.solve(in, out); out.close(); } private s...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 10; long long sb = 0, sg = 0, b[maxn], g[maxn]; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; ++i) scanf("%lld", &b[i]), sb += b[i]; for (int i = 0; i < m; ++i) scanf("%lld", &g[i]), sg += g[i]; sort(b, b + n); s...
Problem: n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 ≤ i ≤ n the minimal number of sweets, which i-th boy presented to some girl is equal ...
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { PrintWriter out = new PrintWriter(System.out); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(""...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int MAXN = 100100; const int MAXM = 2 * MAXN; int n, m; int to[MAXM], nxt[MAXM], head[MAXN], E; struct ST { vector<int> t; vector<int> rev; ST() {} void resize(int sz) { t.resize(sz); rev.resize(sz); } void inverse(int l, int r) { inverse(1, 0, t.s...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int N = 111111; int n, m; int p[N]; int P(int x) { return p[x] == x ? x : p[x] = P(p[x]); } int ta, tb; vector<pair<int, int> > v[N]; int lca[N][20]; int s[N]; int d[N]; int dfs(int ver, int par = -1, int depth = 0) { lca[ver][0] = par; d[ver] = depth; s[ver] = ...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; template <typename T> inline T qmin(const T a, const T b) { return a < b ? a : b; } template <typename T> inline T qmax(const T a, const T b) { return a > b ? a : b; } template <typename T> inline void getmin(T &a, const T b) { if (a > b) a = b; } template <typename T...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int N = 111111; int n, m; int p[N]; int P(int x) { return p[x] == x ? x : p[x] = P(p[x]); } int ta, tb; vector<pair<int, int> > v[N]; int lca[N][20]; int s[N]; int d[N]; int dfs(int ver, int par = -1, int depth = 0) { lca[ver][0] = par; d[ver] = depth; s[ver] = ...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:36777216") #pragma GCC optimize("O2") template <class T> inline void RD(T &); template <class T> inline void OT(const T &); inline int RD() { int x; RD(x); return x; } template <class T> inline T &_RD(T &x) { RD(x); return x; } inli...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int oo = 1047483647, maxn = 100100, N = 131072; int rt, hs[maxn], ans, n, i, j, k, m, q, x, y, dep[maxn], d[maxn], tot, sol, h[maxn], ge, point[maxn], w[maxn], top[maxn], fa[maxn], s[maxn], maxs[maxn], son[maxn]; bool v[maxn]; struct edge { int next[maxn * 2...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; vector<int> g[101010]; vector<int> sta; vector<int> cyc; int u[101010]; int cp[101010]; int dfs(int x, int e) { if (u[x]) { while (1) { cyc.push_back(sta.back()); sta.pop_back(); if (cyc.back() == x) break; } return 1; } else { u[x] = 1...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, m, s, t, sp; vector<int> G[N]; int vis[N], pre[N], suf[N]; int find(int u, int f) { vis[u] = 1; for (int v : G[u]) if (v ^ f) if (vis[v]) return s = v; else if (find(v, u)) return 1; return 0; } int d[N], s...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; namespace Poor { const int MaxiN = 100005; int N, M, Ans; int Pa[MaxiN], Own[MaxiN], At[MaxiN]; int RootMark[MaxiN]; vector<int> Roots; vector<int> Adj[MaxiN]; class TInterval { private: int Size, _S, _T; int *Zero, *One; bool *NeedFlip; void Build(int L, int R) { ...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; int n, q, Time; int start[200000]; int finish[200000]; int head[200000]; int root[200000][20]; int grup[200000]; int go[200000]; int in[200000]; int cy[200000]; int size[200000]; int d[200000]; int wh[200000]; int incycle, outcyle; int bir[2 * 200000], rev[2 * 200000], inv[...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:36777216") #pragma GCC optimize("O2") template <class T> inline void RD(T &); template <class T> inline void OT(const T &); inline int RD() { int x; RD(x); return x; } template <class T> inline T &_RD(T &x) { RD(x); return x; } inli...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> static const int MAXN = 1e5 + 4; template <typename T> inline void swap(T &a, T &b) { static T t; t = a, a = b, b = t; } int n, m; int head[MAXN], dest[MAXN * 2], next[MAXN * 2]; inline void add_edge(int u, int v) { static int w = 0; dest[w] = v; next[w] = head[u]; head[u] = w++; } ...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; struct ST { int n, h; vector<int> t, u, d; void build(int sz) { n = sz; h = sizeof(int) * 8 - __builtin_clz(n); t = vector<int>(2 * n); u = vector<int>(2 * n, 1); for (int i = n - 1; i > 0; i--) u[i] = u[i << 1] + u[i << 1 | 1]; d = vector<int>...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
import java.io.*; import java.math.*; import java.util.*; public class Program { public static class Reader { private final int BUFFER_SIZE = 1 << 12; private int LENGTH = -1; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; ...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int N = 100005, M = 200005, T = 262222, Log = 17; int n, m, h[N], e[M], nxt[M], ans, tot, s, H[N], E[M], Nxt[M], a[N], b[N], size[N], dfn[N], seq[N], top[N], r, sum[T], id[N]; int t1[T], t2[T], tag1[T], tag2[T], fa[N][Log], dep[N]; bool v[N]; char c; int read() { ...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; int n, m; vector<int> lis; struct node { int sum; bool rev; int l, r; } tr[100010 << 2]; int tot, nod[100010]; void build(int &x, int l, int r) { if (!x) x = ++tot; tr[x].rev = tr[x].sum = tr[x].l = tr[x].r = 0; if (l == r) return; int mid = l + r >> 1; buil...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; int n, m; vector<int> G[200006]; int vs[200006]; vector<int> cir; int oc[200006]; bool cfs(int u, int f) { vs[u] = 1; for (int v : G[u]) if (v != f) { if (vs[v]) { cir.push_back(u); oc[u] = oc[v] = 1; return true; } int fg =...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int MOD(1000000009); const int INF((1 << 30) - 1); const int MAXN(100005); vector<int> g[MAXN], tree[MAXN], cycle; bool getcycle = 0, heavy[MAXN], iscycle[MAXN]; int par[MAXN], children[MAXN], numcycle, visited[MAXN], color = 1, root[MAXN], ...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int MAXN = (int)1e5 + 7; const int infint = (int)1e9; int n, m, par[MAXN], a, b, visited[MAXN], dp[MAXN], pos[MAXN], ans = 0, st[MAXN], chain[MAXN], tme, sub[MAXN], h[MAXN], ns[MAXN]; vector<int> path; bool lexi(int u, int v) { int ...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const int INF = 2000000000; const long long BIG = 1446803456761533460LL; const int maxn = 100010; int N, M, C; int st[maxn], cyc[maxn], icyc[maxn], ht[maxn], sz[maxn], par[maxn], head[maxn], rot[maxn]; vector<int> g[maxn], subt[maxn]; bool vi...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 100; vector<int> side[maxn], cir, idx[maxn]; bool incir[maxn], vis[maxn]; int stk[maxn], stktop, top[maxn], heavy[maxn], size[maxn], dfn_cnt, dfn[maxn], f[maxn][20], dep[maxn], cirid[maxn]; struct segment_tree { int sum[maxn << 2], tag[maxn << 2...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using std::swap; const int maxn = 100000 + 10; const int maxlogn = 17 + 1; int n, m, group; int e_tot, e_l[maxn], e_t[maxn << 1], e_next[maxn << 1], e_x[maxn]; int papa[maxn][maxlogn]; int loop_size, loop[maxn]; bool in_loop[maxn], vis[maxn]; int head, tail, queue[maxn]; int size[maxn], up[maxn...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1073741824") using namespace std; const int nmax = 100100; int countEnabled; class SegmentTree { struct node { int val; int inverted; node() { val = inverted = 0; } }; vector<node> t; int n; void push(int v, int tl, int tr) { if (tl != t...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; template <class T> bool umin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> bool umax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } vector<int> cycle, cur, adj[100009]; int vis[100009], parent[100009], c, n...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int MAX = 3e5 + 20; const int INFINITE = INT_MAX; const int MAXNUM = MAX; template <typename T> struct SegTree { struct SegTreeNode { int l, r; T val; T addMark; }; vector<SegTreeNode> nodes; T *arr; int istart, iend, maxIndex; SegTree() {} S...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; template <class A, class B> A cvt(B x) { stringstream ss; ss << x; A y; ss >> y; return y; } int n, m; vector<int> gr[100000]; int c[100000], d[100000], v[100000], cycn; vector<int> cyc; int n2cyc[100000]; void findCycle() { queue<int> q; for (int i = 0; i < (...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int MAX_N = 100010; int n, m; vector<int> graph[MAX_N], tree[MAX_N]; char visited[MAX_N]; stack<int> st; vector<vector<int> > paths; vector<vector<int> > active_nodes; vector<vector<char> > interval_value; int level[MAX_N]; int parent[MAX_N]; int path[MAX_N]; int pos[...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -f; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } template <typename T> i...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; struct segment_tree { vector<int> tree; vector<int> rev; int n; segment_tree() {} segment_tree(int n_) { n = n_; tree.resize(4 * n); rev.resize(4 * n); } void push(int v, int tl, int tr) { if (rev[v] == 0) return; tree[v] = (tr - tl) - tree...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int N = 100010; struct edge { int to, next; } e[N * 2]; int head[N], ecnt; void adde(int from, int to) { e[++ecnt] = {to, head[from]}, head[from] = ecnt; e[++ecnt] = {from, head[to]}, head[to] = ecnt; } struct tcurts { struct node { int l, r, A, B, rev; ...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int SIZE = 100005; class SegTree* ptr; class SegTree { public: int x, y, rev, sum; SegTree *p, *q; SegTree() {} SegTree(int l, int r) : x(l), y(r) { if (x + 1 >= y) p = q = 0; else { int m = (x + y) / 2; p = new (ptr++) SegTree(x, m)...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int N = 100001; vector<int> g[N], cycle, hld[N], st[N], rev[N]; char used[N] = {}, done = 0, revc[N] = {}; int sz[N] = {}, which[N] = {}, status, hldsz = 0, p[N] = {}, szsnm[N] = {}, stc[N] = {}; int up[N][17] = {}, tin[N] = {}, tout[N] = {}, tm = 0, place[N] = {}...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; const int maxk = 17; struct TNode { int L, R; int lc, rc; int sum; bool sw; TNode(int L = 0, int R = 0) : L(L), R(R), lc(-1), rc(-1), sum(0), sw(false) {} }; TNode t[3 * maxn]; int tc = 0; int build(int L, int R) { int node = tc++; t[n...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:36777216") #pragma GCC optimize("O2") template <class T> inline void RD(T &); template <class T> inline void OT(const T &); inline int RD() { int x; RD(x); return x; } template <class T> inline T &_RD(T &x) { RD(x); return x; } inli...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int INF = 0x3fffffff; const int SINF = 0x7fffffff; const long long LINF = 0x3fffffffffffffff; const long long SLINF = 0x7fffffffffffffff; const int MAXN = 100007; const int MAXL = 21; const int MAXS = MAXN * 4; const int MAX = 131072; int n, m; class hldT { public: ...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int N = 100010; struct edge { int to, next; } e[N * 2]; int head[N], ecnt; void adde(int from, int to) { e[++ecnt] = {to, head[from]}, head[from] = ecnt; e[++ecnt] = {from, head[to]}, head[to] = ecnt; } struct tcurts { struct node { int l, r, A, B, rev; ...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int MAX_N_VERTICES = int(1e5) + 10; struct Root; struct Tree; struct Vertex { vector<Vertex*> adj; bool visited; bool inCycle; Root* root; int idx; Vertex *father, *top; Tree* tree; int size, idxInPath; Vertex(int _idx) : idx(_idx), visited(fal...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int maxn = 100 * 1000, maxl = 20; bool mark[maxn], incyc[maxn], cycfound; int h[maxn], par[maxn], nxt[maxn], prv[maxn], where[maxn], pathcnt[maxn], pathid[maxn], pathroot[maxn], lastpathid, cycledge, treeedge, n, m; vector<int> adj[maxn], seg[maxn]; vector<bool> l...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:100000000000000") using namespace std; const long long int INF = 2e9; vector<int> g[200000]; int used[200000], p[200000], is[200000], par[200000][20], root[200000], h[200000], tin[200000], tout[200000], GLOB = 1; vector<int> cycle; int pos_in_cycle[200000]; v...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int N = 200001; const long long mod = 998244353LL; int cycle_end, cycle_start = -1, n, m; int parent[N], depth[N], heavy[N], head[N], pos[N], color[N], tree[4 * N], lazy[4 * N], raiz[N], cur_pos = 1, id[N]; vector<int> lista[N]; vector<int> top; bool dfs_cycle(int...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; int bloque = 100; int n; vector<vector<int> > g; vector<int> altura; bool obtenciclo(int u, int p, vector<int> &visto, vector<int> &ciclo, vector<int> &ant) { if (visto[u]) { int index = 0; for (; ant[index] != u; index++) ; for (; index ...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; int ans = 0; class segment { public: int left, right, count; segment *lson; segment *rson; bool flag; segment() {} segment(int _left, int _right) : left(_left), right(_right), count(0), lson(NULL), rson(NULL), flag(...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
import java.io.*; import java.util.*; /* Solution to http://www.codeforces.com/contest/117/problem/E using link/cut trees. See detailed documentation below. D. Sleator, November 2011 */ public class ESubmit { int n, m, q; int[] ss, ff, first, next, qs, qe; boolean[] on_cycle; int[] parent; ...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; int t, T, L, mb, mdb, ans, d[100005], z[100005], g[100005], o[100005], fa[100005], FA[100005], p[100005 * 2], nxt[100005 * 2]; struct LCtree { int g, t, l[100005], r[100005], z[100005], a[100005], x[100005], y[100005], fa[100005], root[100005]; void put(int i,...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10, mod = 1e9 + 7; const long long inf = 1e18; int val[4 * maxn]; bool lz[4 * maxn]; void get(int l, int r, int id) { if (!lz[id]) return; val[id] = r - l - val[id]; if (r - l > 1) { lz[2 * id] ^= 1; lz[2 * id + 1] ^= 1; } lz[id] = 0...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int tim; int sum; int SUM; int n, m; int h[maxn]; int st[maxn]; int fat[maxn]; int val[maxn]; int sub[maxn]; int type[maxn]; int heavy[maxn]; bool mark[maxn]; int chain[maxn]; int A[maxn], B[maxn]; vector<int> adj[maxn], it; int seg_0[4 * maxn], s...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int N = 214514; int n, m, a, b, t, T, L, mb, mdb, as; int d[N], z[N], g[N], o[N], fa[N], FA[N], p[N * 2], nt[N * 2]; struct d_tr { int g, t, l[N], r[N], z[N], a[N], x[N], y[N], fa[N], ro[N]; void put(int i, int j) { if (i && j) z[i] = !z[i], a[i] = !a[i], swap...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:36777216") #pragma GCC optimize("O2") template <class T> inline void RD(T &); template <class T> inline void OT(const T &); inline int RD() { int x; RD(x); return x; } template <class T> inline T &_RD(T &x) { RD(x); return x; } inli...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:36777216") #pragma GCC optimize("O2") template <class T> inline void RD(T &); template <class T> inline void OT(const T &); inline int RD() { int x; RD(x); return x; } template <class T> inline T &_RD(T &x) { RD(x); return x; } inli...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int maxn = 100001; int n, m, f[maxn], c[maxn][2], fp[maxn], s[maxn], a[maxn], ans, in[maxn], mn[maxn], mn_, fu[maxn], rk[maxn], L[maxn], op, ed; bool mh, nd[maxn], rt, tr[maxn]; vector<int> e[maxn]; int setf(int x) { return x == fu[x] ? x : fu[x] = setf(fu[x]); } ...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e5 + 15; vector<int> adj[maxn]; vector<int> cycle_nodes; pair<int, int> in_cycle = make_pair(-1, -1); int sz[maxn], big_daddy[maxn], par[maxn], st[maxn], en[maxn], cycle_pos[maxn]; bool mark[maxn]; int tim = 0, n, m; bool EX = 0; int CYseg[4 * maxn],...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const int N = 100100; int n, nq; vector<int> g[N]; vector<int> stk, cycle; bool vis[N], incyc[N]; int fl[N], sz[N], tin[N], tmr, up[N], fa[N]; int seg[8 * N]; bool lazy[8 * N]; int pos[N], startCycle, posInCycle[N]; int onEdges; bool cycleOn; inline void dfsInit(int v, int ...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; const long long inf = 1e18; long long n, cr, nx = 0, ttl = 0, ex[100069], od[100069], rt[100069], sbt[100069], pr[100069], an[100069], dh[100069], fh[100069]; vector<long long> al[100069]; bitset<100069> vtd, vtd2; bool r0 = 0, r02 = 0; struct segtree { l...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; template <class A, class B> A cvt(B x) { stringstream ss; ss << x; A y; ss >> y; return y; } int n, m; vector<int> gr[100000]; int c[100000], d[100000], v[100000], cycn; vector<int> cyc; int n2cyc[100000]; void findCycle() { queue<int> q; for (int i = 0; i < (...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; int pct(int x) { return __builtin_popcount(x); } int pct(long long x) { return __builtin_popcountll(x); } int bit(int x) { return 31 - __builtin_clz(x); } int bit(long long x) { return 63 - __builtin_clzll(x); } int cdiv(int a, int b) { return a / b + !(a < 0 || a % b == 0)...
Problem: You are given an undirected connected graph G consisting of n vertexes and n edges. G contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given m queries represented as (v, u) — change the state of all edges on the shortest pat...
#include <bits/stdc++.h> using namespace std; inline int read() { int res = 0; int neg; while (true) { char ch = getchar(); if (ch >= '0' && ch <= '9' || ch == '-') { if (ch == '-') neg = -1; else neg = 1, res = ch - '0'; break; } } while (true) { char ch = ge...