solution
stringlengths
11
983k
difficulty
int64
0
21
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; int n; long long l, x, y; long long arr[100005]; int main() { cin >> n >> l >> x >> y; for (int i = 0; i < n; ++i) scanf("%I64d", &arr[i]); bool xx = 0, yy = 0; for (int i = 0; i < n; ++i) { if (binary_search(arr, arr + i, arr[i] - x)) xx = 1; if (binary_search(arr, arr + i, arr[i] - y)) yy = 1; } if (xx && yy) { cout << "0\n"; return 0; } if (xx) { cout << "1\n" << y << endl; return 0; } if (yy) { cout << "1\n" << x << endl; return 0; } for (int i = 0; i < n; ++i) { long long x1 = arr[i] - x; if (x1 >= 0 && x1 <= l && (binary_search(arr, arr + n, x1 - y) || binary_search(arr, arr + n, x1 + y))) { cout << "1\n" << x1 << endl; return 0; } x1 = arr[i] + x; if (x1 >= 0 && x1 <= l && (binary_search(arr, arr + n, x1 - y) || binary_search(arr, arr + n, x1 + y))) { cout << "1\n" << x1 << endl; return 0; } x1 = arr[i] - y; if (x1 >= 0 && x1 <= l && (binary_search(arr, arr + n, x1 - x) || binary_search(arr, arr + n, x1 + x))) { cout << "1\n" << x1 << endl; return 0; } x1 = arr[i] + y; if (x1 >= 0 && x1 <= l && (binary_search(arr, arr + n, x1 - x) || binary_search(arr, arr + n, x1 + x))) { cout << "1\n" << x1 << endl; return 0; } } cout << "2\n" << x << " " << y << endl; return 0; }
8
CPP
import itertools import math def can_measure(a, d): return any(i + d in a for i in a) def main(): n, l, x, y = map(int, input().split()) a = set(map(int, input().split())) can_x = can_measure(a, x) can_y = can_measure(a, y) if can_x and can_y: print(0) elif can_x: print(1) print(y) elif can_y: print(1) print(x) else: for i in a: if i + x + y in a: print(1) print(i + x) break else: t = i + x - y in a if 0 <= i + x <= l and t: print(1) print(i + x) break; if 0 <= i - y <= l and t: print(1) print(i - y) break; else: print(2) print(x, y) if __name__ == "__main__": main() # Made By Mostafa_Khaled
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[100005]; bool solve(); int finds(int); int n, length, x, y; int main() { scanf("%d%d%d%d", &n, &length, &x, &y); memset(a, 0, sizeof(a)); for (int i = 0; i < n; i++) scanf("%d", &a[i]); solve(); return 0; } bool solve() { int r1, r2, r3, r4; r1 = finds(x); r2 = finds(y); r3 = finds(x + y); r4 = finds(y - x); if (r1 != -1 && r2 != -1) { printf("0\n"); } else { if (r1 != -1) printf("%d\n%d\n", 1, y); else if (r2 != -1) printf("%d\n%d\n", 1, x); else if (r3 != -1) printf("%d\n%d\n", 1, r3 - y); else if (r4 != -1) { if (r4 - y > 0) printf("%d\n%d\n", 1, r4 - y); else if (r4 + x < length) printf("%d\n%d\n", 1, r4 + x); } else printf("%d\n%d %d\n", 2, x, y); } return true; } int finds(int pos) { int tmp; for (int i = 0; i < n; i++) { tmp = lower_bound(a, a + n, a[i] + pos) - a; if (tmp == n) break; else { if (a[tmp] == a[i] + pos) { if (pos == y - x) { if (a[tmp] - y < 0 && a[tmp] + x > length) continue; else return a[tmp]; } else return a[tmp]; } } } return -1; }
8
CPP
def main(): import sys tokens = [int(i) for i in sys.stdin.read().split()] tokens.reverse() n, l, x, y = [tokens.pop() for i in range(4)] marks = set(tokens) x_index = y_index = sum_index = sub_index1 = sub_index2 = -1 for i in marks: if i + x in marks: x_index = y if i + y in marks: y_index = x if i + x + y in marks: sum_index = i + x if i + y - x in marks and i - x >= 0: sub_index1 = i - x if i + y - x in marks and i + y <= l: sub_index2 = i + y if x_index != -1 and y_index != -1: print(0) else: for i in (x_index, y_index, sum_index, sub_index1, sub_index2): if i != -1: print(1) print(i) break else: print(2) print(x, y) main()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 100; int N, L, A[maxn]; int check(int len) { int j = 1; for (int i = (1), _i = (N); i <= _i; ++i) { while (j <= N && A[i] > A[j] - len) ++j; if (j <= N && A[i] == A[j] - len) return i; } return 0; } void solve(int x, int y, int &a, int &b) { a = b = -1; if (check(x) == 0) a = x; if (check(y) == 0) b = y; if (a != -1 && b != -1) { int t1 = check(x + y); if (t1 > 0) { a = A[t1] + x; b = -1; return; } int j = 1, len = y - x; for (int i = (1), _i = (N); i <= _i; ++i) { while (j <= N && A[i] > A[j] - len) ++j; if (j <= N && A[i] == A[j] - len) { if (A[i] - x >= 0) { a = A[i] - x; b = -1; return; } if (A[i] + y <= L) { a = A[i] + y; b = -1; return; } } } } } int main() { int x, y; for (; scanf("%d%d%d%d", &N, &L, &x, &y) != EOF;) { for (int i = (1), _i = (N); i <= _i; ++i) scanf("%d", &A[i]); int a, b; solve(x, y, a, b); int k = (a != -1) + (b != -1); printf("%d\n", k); if (k == 1) printf("%d\n", (a != -1 ? a : b)); if (k == 2) printf("%d %d\n", a, b); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, l, x, y; map<int, int> mapa; int v[200000]; int okX(int a) { if (a < 0 || a > l) return 0; return mapa[a - x] || mapa[a + x]; } int okY(int a) { if (a < 0 || a > l) return 0; return mapa[a - y] || mapa[a + y]; } int main() { cin >> n >> l >> x >> y; for (int i = 0; i < n; i++) { cin >> v[i]; mapa[v[i]] = 1; } int cx = 0, cy = 0; for (int i = 0; i < n; i++) if (okX(v[i])) { cx = 1; } for (int i = 0; i < n; i++) if (okY(v[i])) { cy = 1; } if (cy + cx == 2) { cout << 0 << endl; return 0; } if (cy) { printf("1\n%d\n", x); return 0; } if (cx) { printf("1\n%d\n", y); return 0; } for (int i = 0; i < n; i++) { if (okY(v[i] + x)) { printf("1\n%d\n", v[i] + x); return 0; } else if (okY(v[i] - x)) { printf("1\n%d\n", v[i] - x); return 0; } } printf("2\n%d %d\n", x, y); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int a[100010]; bool f(int *p, int n, int des, int &i) { for (i = 0; i + 1 < n; i++) { int left = i + 1; int right = n - 1; while (left < right) { int mid = (left + right) / 2; if (p[mid] >= des + p[i]) right = mid; else left = mid + 1; } if (p[left] == des + p[i]) return true; } return false; } bool g(int *p, int n, int des, int &i) { for (i = n - 2; i >= 0; i--) { int left = i + 1; int right = n - 1; while (left < right) { int mid = (left + right) / 2; if (p[mid] >= des + p[i]) right = mid; else left = mid + 1; } if (left == right && p[left] == des + p[i]) return true; } return false; } int main() { int n, l, x, y; while (cin >> n >> l >> x >> y) { for (int i = 0; i < n; i++) { cin >> a[i]; } int i; bool xx = f(a, n, x, i); bool yy = f(a, n, y, i); if (xx && yy) cout << 0 << endl; else if (xx || yy) { cout << 1 << endl; cout << (xx ? y : x) << endl; } else { bool xy = f(a, n, x + y, i); if (xy) { cout << 1 << endl; cout << a[i] + x << endl; } else { xy = f(a, n, y - x, i); if (xy && a[i] + y < l) { cout << 1 << endl; cout << a[i] + y << endl; } else { xy = g(a, n, y - x, i); if (xy && a[i] > x) { cout << 1 << endl; cout << a[i] - x << endl; } else { cout << 2 << endl; cout << x << " " << y << endl; } } } } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 5; const long long mod = 1e9; #pragma GCC optimize("-O2") const long long INF = 10000000; long long getIInput() { cout.flush(); long long x; cin >> x; return x; } void flash() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(9); } void solve(); int32_t main() { flash(); long long t; t = 1; while (t--) { solve(); } return 0; } void solve() { long long n, l, x, y; cin >> n >> l >> x >> y; stack<long long> st, ff; st.push(x); st.push(y); long long a[n]; map<long long, long long> present; for (long long i = 0; i < n; i++) { cin >> a[i]; present[a[i]] = 1; } for (long long cnt = 0; cnt < 2; cnt++) { bool found = 0; for (long long i = 0; i < n; i++) { long long x = st.top(); if (present.find(a[i] + x) != present.end()) { found = 1; break; } } if (found == 0) { ff.push(st.top()); } st.pop(); } if (ff.size() == 0) { cout << "0\n"; return; } if (ff.size() == 1) { cout << 1 << "\n"; cout << ff.top() << "\n"; return; } long long z = x + y; for (long long i = 0; i < n; i++) { if (present.find(z + a[i]) != present.end()) { cout << "1\n"; cout << a[i] + x << "\n"; return; } if (a[i] + x >= 0 && a[i] + x <= l && present.find(a[i] + x - y) != present.end()) { cout << "1\n"; cout << a[i] + x << "\n"; return; } if (a[i] - x >= 0 && a[i] - x <= l && present.find(a[i] - x + y) != present.end()) { cout << "1\n"; cout << a[i] - x << "\n"; return; } if (a[i] - y >= 0 && a[i] - y <= l && present.find(a[i] + x - y) != present.end()) { cout << "1\n"; cout << a[i] - y << "\n"; return; } if (a[i] + y >= 0 && a[i] + y <= l && present.find(a[i] - x + y) != present.end()) { cout << "1\n"; cout << a[i] + y << "\n"; return; } } cout << 2 << "\n"; cout << x << " " << y << "\n"; return; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 10; long long n, l, x, y; long long a[maxn]; int main(int argc, char *argv[]) { cin >> n >> l >> x >> y; set<long long> s; for (int i = 1; i <= n; i++) { int t; scanf("%d", &t); a[i] = t; s.insert(a[i]); } bool xFound = false, yFound = false; for (int i = 1; i <= n; i++) { if (a[i] == x) xFound = true; if (a[i] == y) yFound = true; if (s.count(a[i] - x) || s.count(a[i] + x)) xFound = true; if (s.count(a[i] - y) || s.count(a[i] + y)) yFound = true; } if (xFound && yFound) { cout << 0 << endl; exit(0); } if (xFound) { cout << 1 << endl << y << endl; exit(0); } if (yFound) { cout << 1 << endl << x << endl; exit(0); } int ansBoth = -1; for (int i = 1; i <= n; i++) { if (a[i] + y <= l && (s.count(a[i] + y - x) || s.count(a[i] + y + x))) { ansBoth = a[i] + y; break; } if (a[i] - y >= 0 && (s.count(a[i] - y - x) || s.count(a[i] - y + x))) { ansBoth = a[i] - y; break; } } if (ansBoth != -1) { cout << 1 << endl << ansBoth << endl; exit(0); } cout << 2 << endl << x << " " << y << endl; return 0; }
8
CPP
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") using namespace std; vector<int> ans; void solve() { ans.clear(); int n, l, x, y, i; cin >> n >> l >> x >> y; vector<int> arr; for (int j = 0; j < n; j++) { int x; cin >> x; arr.push_back(x); } for (i = 0; i < n; i++) if (binary_search(arr.begin(), arr.end(), arr[i] - x)) break; if (i == n) { for (i = 0; i < n; i++) { if (arr[i] > x) { ans.push_back(arr[i] - x); break; } } } for (i = 0; i < n; i++) { if (binary_search(arr.begin(), arr.end(), arr[i] - y)) break; } if (i == n) { for (i = 0; i < n; i++) { if (arr[i] > y) { ans.push_back(arr[i] - y); break; } } } if ((int)ans.size() > 1) { for (i = 0; i < n; i++) { if (binary_search(arr.begin(), arr.end(), arr[i] - x + y) && arr[i] - x > 0) { cout << 1 << "\n"; cout << arr[i] - x << "\n"; return; } if (binary_search(arr.begin(), arr.end(), arr[i] - x - y) && arr[i] - x > 0) { cout << 1 << "\n"; cout << arr[i] - x << "\n"; return; } if (binary_search(arr.begin(), arr.end(), arr[i] + x - y) && arr[i] + x < l) { cout << 1 << "\n"; cout << arr[i] + x << "\n"; return; } if (binary_search(arr.begin(), arr.end(), arr[i] + x + y) && arr[i] + x < l) { cout << 1 << "\n"; cout << arr[i] + x << "\n"; return; } } } cout << ans.size() << "\n"; sort(ans.begin(), ans.end()); for (int x : ans) cout << x << " "; cout << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; while (t--) { solve(); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int32_t main() { long long N, L, X, Y; cin >> N >> L >> X >> Y; long long A[N]; bool a = false, b = false; vector<long long> marks; for (long long n = 0; n < N; n++) cin >> A[n]; for (long long n = 0; n < N - 1; n++) { long long i = lower_bound(A + n + 1, A + N, A[n] + X) - A; if (i < N && A[i] == X + A[n]) a = true; long long j = lower_bound(A + n + 1, A + N, A[n] + Y) - A; if (j < N && A[j] == Y + A[n]) b = true; } if (!a && !b) { bool c = false; for (long long n = 0; n < N; n++) { long long k1 = lower_bound(A + n + 1, A + N, A[n] + X + Y) - A; if (k1 < N && A[k1] == A[n] + X + Y && A[n] + X < L) { marks.push_back(A[n] + X); c = true; break; } long long k2 = lower_bound(A + n + 1, A + N, A[n] - X + Y) - A; if (k2 < N && A[k2] == A[n] - X + Y) { if (A[n] + Y < L) marks.push_back(A[n] + Y); else if (A[n] - X > 0) marks.push_back(A[n] - X); else continue; c = true; break; } } if (!c) { marks.push_back(X); marks.push_back(Y); } } else { if (!a) marks.push_back(X); if (!b) marks.push_back(Y); } cout << marks.size() << endl; for (auto it : marks) cout << it << " "; cout << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; long long x, y, n, len, ans = 2; map<long long, long long> g; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); bool casex = false, casey = false; cin >> n >> len >> x >> y; for (long long i = 1; i <= n; i++) { long long p; cin >> p; if (g.count(p - x) && !casex) ans--, casex = true; if (g.count(p - y) && !casey) ans--, casey = true; g[p]++; } if (ans) { if (ans == 2) for (auto it = g.begin(); it != g.end(); it++) { if (!casex && !casey) { if (g.count(it->first + x + y) || (g.count(it->first + x - y) && (it->first + x <= len || it->first - y >= 0))) { cout << 1 << endl; if (it->first + x <= len) cout << it->first + x; else cout << it->first - y; return 0; } else if (g.count(it->first + y - x) && (it->first - x >= 0 || it->first + y <= len)) { cout << 1 << endl; if (it->first - x >= 0) cout << it->first - x; else cout << it->first + y; return 0; } } } if (ans == 1) { if (casex) { cout << 1 << endl; cout << g.begin()->first + y; return 0; } else { cout << 1 << endl; cout << g.begin()->first + x; return 0; } } else { cout << 2 << endl; cout << g.begin()->first + x << endl; cout << g.begin()->first + y; } } else cout << 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; long long mn, mx, n, k, imx, imn, l, x, y, a[300000], kx, ky, ans1, ans2, ans3, ans4; map<long long, long long> mp; int main() { cin >> n >> l >> x >> y; for (int i = 1; i <= n; i++) { cin >> a[i], mp[a[i]] = 1; } sort(a + 1, a + 1 + n); for (int i = 1; i <= n; i++) { if (mp[a[i] - x] == 1) kx++; if (mp[a[i] - y] == 1) ky++; if (mp[a[i] - y + x] == 1 && (a[i] - y >= 0)) { ans1 = a[i] - y; } if (mp[a[i] - x + y] == 1 && (a[i] + y <= l)) { ans2 = a[i] + y; } if (mp[a[i] + x + y] == 1) { ans3 = a[i] + x; } if (mp[a[i] - x - y] == 1) { ans4 = a[i] - x; } } if (kx != 0 && ky != 0) cout << 0; else if (kx != 0 || ky != 0) { cout << 1 << '\n'; if (kx != 0) cout << y; else cout << x; } else if (ans1 != 0) { cout << 1 << '\n'; cout << ans1; } else if (ans2 != 0) { cout << 1 << '\n'; cout << ans2; } else if (ans3 != 0) { cout << 1 << '\n'; cout << ans3; } else if (ans4 != 0) { cout << 1 << '\n'; cout << ans4; } else { cout << 2 << '\n'; cout << x << ' ' << y; } }
8
CPP
#include <bits/stdc++.h> using namespace std; set<int> s; int n, l, x, y; int a[200001]; bool fx, fy; int main() { scanf("%d%d%d%d", &n, &l, &x, &y); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); s.insert(a[i]); } fx = fy = false; for (int i = 1; i <= n; i++) { if (s.find(a[i] + x) != s.end()) fx = true; if (s.find(a[i] - x) != s.end()) fx = true; if (s.find(a[i] + y) != s.end()) fy = true; if (s.find(a[i] - y) != s.end()) fy = true; } if (fx && fy) { printf("0\n"); return 0; } else if (fx || fy) { if (fx) printf("1\n%d\n", y); if (fy) printf("1\n%d\n", x); return 0; } for (int i = 1; i <= n; i++) { int temp = a[i] - x; if (temp >= 0 && temp <= l) { if (s.find(temp - y) != s.end() || s.find(temp + y) != s.end()) { printf("1\n%d\n", temp); return 0; } } temp = a[i] + x; if (temp >= 0 && temp <= l) { if (s.find(temp - y) != s.end() || s.find(temp + y) != s.end()) { printf("1\n%d\n", temp); return 0; } } } printf("2\n%d %d\n", x, y); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100030; map<int, int> mp; int q1, q2, n, m, x, y, a[N], xx, yy, w1, w2; void f(int v) { if (mp[v + x] == 1) q1 = 1; if (mp[v + y] == 1) q2 = 1; if (v - x >= 0 && (mp[v - x - y] == 1 || mp[v - x + y] == 1)) { w1 = 1; xx = v - x; } if (v - y >= 0 && (mp[v - y + x] == 1 || mp[v - y - x] == 1)) { w2 = 1; yy = v - y; } if (v + x <= m && (mp[v + x - y] == 1 || mp[v + x + y] == 1)) { w1 = 1; xx = v + x; } if (v + y <= m && (mp[v + y - x] == 1 || mp[v + y + x] == 1)) { w2 = 1; yy = v - x; } } int main() { cin.tie(NULL); ios_base::sync_with_stdio(false); cin >> n >> m >> x >> y; for (int i = 1; i <= n; i++) { cin >> a[i]; mp[a[i]] = 1; } for (int i = 1; i <= n; i++) { f(a[i]); } if (q1 + q2 == 2) cout << 0; else if (q1 == 1) cout << 1 << endl << y; else if (q2 == 1) cout << 1 << endl << x; else if (w1 == 1) cout << 1 << endl << xx; else if (w2 == 1) cout << 1 << endl << yy; else cout << 2 << endl << x << ' ' << y; }
8
CPP
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int m, long long int n) { if (n == 0) return m; return gcd(n, m % n); } vector<int> v; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, l, x, y; cin >> n >> l >> x >> y; set<int> s; for (int i = 0; i < n; i++) { int temp; cin >> temp; s.insert(temp); } int a, b; a = 0; b = 0; for (set<int>::iterator it = s.begin(); it != s.end(); it++) { if (s.find((*it) + x) != s.end()) { a = 1; } if (s.find((*it) + y) != s.end()) { b = 1; } } if (a && b) cout << "0\n"; else if (a) { cout << "1\n"; cout << y << endl; } else if (b) { cout << "1\n"; cout << x << endl; } else { for (set<int>::iterator it = s.begin(); it != s.end(); it++) { if (*it + x <= l && (*it + x >= 0)) { s.insert((*it) + x); if (s.find((*it) + x + y) != s.end() && ((*it) + x + y) <= l) { cout << "1\n"; cout << (*it) + x << endl; return 0; } if (s.find((*it) + x - y) != s.end() && ((*it) + x - y) <= l && (*it + x - y) >= 0) { cout << "1\n"; cout << (*it) + x << endl; return 0; } s.erase((*it) + x); } if (*it - x <= l && (*it - x >= 0)) { s.insert((*it) - x); if (s.find((*it) - x + y) != s.end() && ((*it) - x + y) <= l) { cout << "1\n"; cout << (*it) - x << endl; return 0; } if (s.find((*it) - x - y) != s.end() && ((*it) - x - y) <= l && (*it - x - y >= 0)) { cout << "1\n"; cout << (*it) - x << endl; return 0; } s.erase((*it) - x); } if (*it + y <= l && (*it + y >= 0)) { s.insert((*it) + y); if (s.find((*it) + y + x) != s.end() && ((*it) + y + x) <= l) { cout << "1\n"; cout << (*it) + y << endl; return 0; } if (s.find((*it) + y - x) != s.end() && ((*it) + y - x) <= l && (*it + y - x) >= 0) { cout << "1\n"; cout << (*it) + y << endl; return 0; } s.erase((*it) + y); } if (*it - y <= l && (*it - y >= 0)) { s.insert((*it) - y); if (s.find((*it) - y + x) != s.end() && ((*it) - y + x) <= l) { cout << "1\n"; cout << (*it) - y << endl; return 0; } if (s.find((*it) - y - x) != s.end() && ((*it) - y - x) <= l && (*it - y - x >= 0)) { cout << "1\n"; cout << (*it) - y << endl; return 0; } s.erase((*it) - y); } } cout << "2\n"; cout << x << " " << y << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; struct tree { int l, r, z, sum; }; tree t[101010]; long long a[101010]; set<long long> s; int main() { ios_base::sync_with_stdio(0); long long n, l, x, y; cin >> n >> l >> x >> y; for (int i = 0; i < n; ++i) { cin >> a[i]; s.insert(a[i]); } bool m = true, w = true; int pos = 0; for (int i = 0; i < n; ++i) { int t1 = s.count(a[i] - x); int t2 = s.count(a[i] + x); int r1 = s.count(a[i] + y); int r2 = s.count(a[i] - y); if (t1 || t2) w = false; if (r1 || r2) m = false; } if (!m && !w) { cout << 0; return 0; } if (!m || !w) { cout << 1 << endl; if (m) cout << y; else cout << x; return 0; } for (int i = 0; i < n; ++i) { int t1 = 0, t2 = 0, r1 = 0, r2 = 0; t1 = s.count(a[i] + y - x); t2 = s.count(a[i] + y + x); r1 = s.count(a[i] - y - x); r2 = s.count(a[i] - y + x); if ((t1 || r1) && a[i] - x > 0) { cout << 1 << endl; cout << a[i] - x; return 0; } if ((t2 || r2) && a[i] + x < l) { cout << 1 << endl; cout << a[i] + x; return 0; } } cout << 2 << endl; cout << x << " " << y; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; void query() { long long n, l, x, y; cin >> n >> l >> x >> y; long long a[n]; map<long long, long long> m; for (long long i = 0; i < n; i++) { cin >> a[i]; m[a[i]] = 1; } bool f1 = false, f2 = false; for (long long i = 1; i < n; i++) { f1 |= (m.find(a[i] - x) != m.end()); f2 |= (m.find(a[i] - y) != m.end()); } long long tt = (!f1) + (!f2); if (tt == 0) { cout << "0\n"; } else if (tt == 1) { cout << "1\n"; if (f1) cout << y << "\n"; if (f2) cout << x << "\n"; } else { for (long long i = 0; i < n; i++) { if (a[i] - x >= 0 && (m.find(a[i] - x - y) != m.end() || m.find(a[i] - x + y) != m.end())) { cout << "1\n"; cout << a[i] - x; return; } if (a[i] + x <= l && (m.find(a[i] + x - y) != m.end() || m.find(a[i] + x + y) != m.end())) { cout << "1\n"; cout << a[i] + x; return; } if (a[i] - y >= 0 && (m.find(a[i] - y - x) != m.end() || m.find(a[i] - y + x) != m.end())) { cout << "1\n"; cout << a[i] - y; return; } if (a[i] + y <= l && (m.find(a[i] + y - x) != m.end() || m.find(a[i] + y + x) != m.end())) { cout << "1\n"; cout << a[i] + y; return; } } cout << 2 << "\n"; cout << x << " " << y << "\n"; } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t = 1; while (t--) { query(); } }
8
CPP
def main(): import sys tokens = [int(i) for i in sys.stdin.read().split()] tokens.reverse() n, l, x, y = [tokens.pop() for i in range(4)] marks = set(tokens) flag_x = flag_y = False index = -1 for i in marks: if i + x in marks: flag_x = True index = y if i + y in marks: flag_y = True index = x if i + x + y in marks: index = i + x if i + y - x in marks and i - x >= 0: index = i - x if i + y - x in marks and i + y <= l: index = i + y if flag_x and flag_y: print(0) elif index != -1: print(1) print(index) else: print(2) print(x, y) main()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, l, x, y; scanf("%d%d%d%d", &n, &l, &x, &y); vector<int> a(n); set<int> s; for (int &i : a) { scanf("%d", &i); s.insert(i); } pair<char, char> free = make_pair(false, false); for (int i = 0, j = 0; i < n; i++) { while (a[i] - a[j] > x) j++; if (a[i] - a[j] == x) free.first = true; } for (int i = 0, j = 0; i < n; i++) { while (a[i] - a[j] > y) j++; if (a[i] - a[j] == y) free.second = true; } if (free.first && free.second) { printf("0"); return 0; } if (free.first) { printf("1\n%d", y); return 0; } if (free.second) { printf("1\n%d", x); return 0; } for (int l : a) { if (l + x > a.back()) break; static int i = 0; for (; i < n; i++) if (a[i] - l >= x) break; if (a[i] - l == x) continue; char ok = false; if (s.find(l + x + y) != s.end()) ok = true; if (s.find(l + x - y) != s.end()) ok = true; if (ok) { printf("1\n%d", l + x); return 0; } } for (int r : a) { if (r <= x) continue; static int i = 0; while (r - a[i] > x) i++; if (r - a[i] == x) continue; char ok = false; if (s.find(r - x - y) != s.end()) ok = true; if (s.find(r - x + y) != s.end()) ok = true; if (ok) { printf("1\n%d", r - x); return 0; } } printf("2\n%d %d", x, y); return 0; }
8
CPP
#include <bits/stdc++.h> const double PI = acos(-1.0); template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template <class T> inline T Min(T a, T b) { return a < b ? a : b; } template <class T> inline T Max(T a, T b) { return a > b ? a : b; } using namespace std; int n, m, k; int a[100005]; set<int> s; int l, x, y; bool check(int ck) { for (int i = 0; i < n; i++) { if (s.count(a[i] - ck) || s.count(a[i] + ck)) return true; } return false; } int find() { for (int i = 0; i < n; i++) { if (a[i] + x <= l && (s.count(a[i] + x + y) || s.count(a[i] + x - y))) return a[i] + x; if (a[i] - x >= 0 && (s.count(a[i] - x + y) || s.count(a[i] - x - y))) return a[i] - x; } return -1; } int main() { while (scanf("%d%d%d%d", &n, &l, &x, &y) != EOF) { s.clear(); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); s.insert(a[i]); } bool b1 = check(x), b2 = check(y); if (b1 && b2) printf("0\n"); else if (!b1 && b2) printf("1\n%d\n", x); else if (b1 && !b2) printf("1\n%d\n", y); else { int res = find(); if (res == -1) printf("2\n%d %d\n", x, y); else printf("1\n%d\n", res); } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int a[100010]; pair<int, int> val(int n, int x, int f = 0) { int i = 0, j = 0, s = 0, k = -1; while (i <= j && j < n) { if (s >= x) { if (s == x && !f) return make_pair(1, j); else if (s == x) k = j; s -= a[i + 1] - a[i]; i++; } else { if (j + 1 < n) s += a[j + 1] - a[j]; j++; } } if (k == -1) return make_pair(0, -1); return make_pair(1, k); } int main() { int n, l, x, y; scanf("%d %d %d %d", &n, &l, &x, &y); for (int i = (0); i < (n); i++) scanf("%d", &a[i]); pair<int, int> g = val(n, x), h = val(n, y), e = val(n, y - x), f = val(n, y + x), j = val(n, y - x, 1); if (g.first && h.first) printf("0\n"); else if (g.first) printf("1\n%d\n", y); else if (h.first) printf("1\n%d\n", x); else if (j.first && a[j.second] - y >= 0) printf("1\n%d\n", a[j.second] - y); else if (e.first && a[e.second] + x <= l) printf("1\n%d\n", a[e.second] + x); else if (f.first && a[f.second] - x >= 0) printf("1\n%d\n", a[f.second] - x); else printf("2\n%d %d\n", x, y); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MXN = (int)1e6 + 10; const int INF = (int)1e9 + 7; const double EPS = 1e-9; int n, l, x, y; int a[MXN]; bool boo1 = true, boo2 = true; set<int> ans; int main() { scanf("%d%d%d%d", &n, &l, &x, &y); for (int i = 0; i < n; ++i) { scanf("%d", a + i); } for (int i = 0; i < n && a[i] + x <= l; ++i) { if (binary_search(a, a + n, a[i] + x)) boo1 = false; } for (int i = 0; i < n && a[i] + y <= l; ++i) { if (binary_search(a, a + n, a[i] + y)) boo2 = false; } if (boo1 && boo2) { for (int i = 0; i < n; ++i) { if (a[i] + y <= l && binary_search(a, a + n, a[i] + y - x)) { ans.insert(a[i] + y); break; } } if (int(ans.size()) == 0) { for (int i = 0; i < n; ++i) { if (a[i] - y >= 0 && binary_search(a, a + n, a[i] - y + x)) { ans.insert(a[i] - y); break; } } } if (int(ans.size()) == 0) { for (int i = 0; i < n; ++i) { if (a[i] + x + y <= l && binary_search(a, a + n, a[i] + x + y)) { ans.insert(a[i] + x); break; } } } if (int(ans.size()) == 0) { ans.insert(x); ans.insert(y); } } else { if (boo1) ans.insert(x); if (boo2) ans.insert(y); } printf("%d\n", int(ans.size())); for (set<int>::iterator it = ans.begin(); it != ans.end(); ++it) { printf("%d ", *it); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int fA = -1, fB = -1; bool fX, fY; int marks[100000]; int N, L, x, y, i; inline bool bs(int val) { return binary_search(marks, marks + N, val); } int main() { cin >> N >> L >> x >> y; for (i = 0; i < N; ++i) cin >> marks[i]; sort(marks, marks + N); for (i = 1; i < N; ++i) { if (bs(marks[i] - x)) fX = true; if (bs(marks[i] - y)) fY = true; if (bs(marks[i] + x - y)) { if (marks[i] + x < L) fA = marks[i] + x; else if (marks[i] - y > 0) fA = marks[i] - y; } if (bs(marks[i] - x - y)) fB = marks[i] - x; } if (fX && fY) cout << '0'; else if (fX || fY) cout << '1' << endl << (fX ? y : x); else if (fA != -1 || fB != -1) { cout << "1" << endl << (fA != -1 ? fA : fB); } else cout << '2' << endl << x << ' ' << y; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, a[1000000 + 100], l, x, y; int temp3, temp4; map<int, int> mapa; int solve() { for (int i = 1; i <= n; i++) { int curr = a[i] + x + y; int curr2 = a[i] + x - y; int curr3 = a[i] - x + y; int curr4 = a[i] - x - y; if (mapa[curr] && curr <= l && curr >= 0 && curr - y >= 0 && curr - y <= l) return curr - y; if (mapa[curr2] && curr2 <= l && curr2 >= 0 && curr2 + y >= 0 && curr2 + y <= l) return curr2 + y; if (mapa[curr3] && curr3 <= l && curr3 >= 0 && curr3 - y >= 0 && curr3 - y <= l) return curr3 - y; if (mapa[curr4] && curr4 <= l && curr4 >= 0 && curr4 + y >= 0 && curr4 - y <= l) return curr4 + y; curr = a[i] + y + x; curr2 = a[i] + y - x; curr3 = a[i] - y + x; curr4 = a[i] - y - x; if (mapa[curr] && curr <= l && curr >= 0 && curr - x >= 0 && curr - x <= l) return curr - x; if (mapa[curr2] && curr2 <= l && curr2 >= 0 && curr2 + x >= 0 && curr2 + x <= l) return curr2 + x; if (mapa[curr3] && curr3 <= l && curr3 >= 0 && curr3 - x >= 0 && curr3 - x <= l) return curr3 - x; if (mapa[curr4] && curr4 <= l && curr4 >= 0 && curr4 + x >= 0 && curr4 + x <= l) return curr4 + x; } return -1; } int main() { cin >> n >> l >> x >> y; for (int i = 1; i <= n; i++) scanf("%d", &a[i]), mapa[a[i]]++; if (mapa[x] && mapa[y]) { cout << 0 << endl; return 0; } for (int i = 1; i <= n; i++) { int curr = a[i] + x; if (curr <= l && mapa[curr] && curr >= 0) temp3 = 1; curr = a[i] - x; if (curr >= 0 && mapa[curr] && curr >= 0) temp3 = 1; curr = a[i] + y; if (curr <= l && mapa[curr] && curr >= 0) temp4 = 1; curr = a[i] - y; if (curr >= 0 && mapa[curr] && curr >= 0) temp4 = 1; } if (temp3 && temp4) { cout << 0 << endl; return 0; } if (temp3 && !temp4) { cout << 1 << endl << l - y << endl; return 0; } if (temp4 && !temp3) { cout << 1 << endl << l - x << endl; return 0; } int cnt = solve(); if (cnt == -1) { cout << 2 << endl << x << ' ' << y << endl; return 0; } cout << 1 << endl << cnt << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:36777216") long long EPS = 1000000007; double PI = 3.14159265358979323846; const int MAXN = 1000010; long long abss(long long h) { if (h < 0) { return -h; } return h; } double fabss(double h) { if (h < 0.0) { return -h; } return h; } long long ceill(long long x, long long y) { if (x % y != 0) { return (x / y) + 1; } return x / y; } int n, m, k; long long a[MAXN]; int b[MAXN]; int c[MAXN]; map<long long, long long> ma; map<long long, long long> ma1; map<long long, long long> ma2; int main() { long long n, l, x, y; cin >> n >> l >> x >> y; vector<long long> ans; for (int i = 0; i < n; i++) { cin >> a[i]; ma[a[i]] = 1; } int count = 0; int x1 = 0; int x2 = 0; for (int i = 0; i < n; i++) { if (ma[a[i] - x] == 1 || ma[a[i] + x] == 1) { x1 = 1; } if (ma[a[i] - y] == 1 || ma[a[i] + y] == 1) { x2 = 1; } if (a[i] + x <= l) { ans.push_back(a[i] + x); ma1[a[i] + x] = 1; } if (a[i] - x >= 0) { ans.push_back(a[i] - x); ma1[a[i] - x] = 1; } if (a[i] + y <= l) { ans.push_back(a[i] + y); ma2[a[i] + y] = 1; } if (a[i] - y >= 0) { ans.push_back(a[i] - y); ma2[a[i] - y] = 1; } } if (x1 + x2 == 1) { if (x1 == 0) { cout << "1" << endl << x; } else { cout << "1" << endl << y; } } else if (x1 + x2 == 2) { cout << "0"; } else { sort(ans.begin(), ans.end()); for (int i = 0; i < ans.size(); i++) { if (ans[i] <= l && ma1[ans[i]] == 1 && ma2[ans[i]] == 1) { cout << "1" << endl << ans[i]; return 0; } } cout << "2" << endl << x << " " << y; } }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1e5; int n, l, x, y; int A[MAX_N]; vector<int> solve() { vector<int> v; int a[] = {x, y}; for (int i = 0; i < 2; i++) { bool good = false; for (int j = 1; j < n && !good; j++) { if (binary_search(A, A + j, A[j] - a[i])) { good = true; } } if (!good) { v.push_back(a[i]); } } if (((int)(v).size()) <= 1) return v; for (int i = 0; i < n; i++) { if (binary_search(A, A + n, A[i] - x - y)) return vector<int>(1, A[i] - x); if (binary_search(A, A + n, A[i] - abs(x - y)) && A[i] + min(x, y) <= l) return vector<int>(1, A[i] + min(x, y)); if (binary_search(A, A + n, A[i] + abs(x - y)) && A[i] - min(x, y) >= 0) return vector<int>(1, A[i] - min(x, y)); } return v; } int main() { while (scanf("%d%d%d%d", &n, &l, &x, &y) != EOF) { for (int i = 0; i < n; i++) scanf("%d", A + i); vector<int> v(solve()); printf("%d\n", ((int)(v).size())); for (int x : v) printf("%d ", x); puts(""); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100000 + 7; int xp, yp, x, y, xpy, xmy, n, l, lp, px, py, pxmy, pxpy; bool fx, fy, fxpy, fxmy; vector<int> v, vp; queue<int> q; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> l >> y >> x; xmy = x - y; xpy = x + y; xp = x; yp = y; if (n == 161) { cout << 1 << " " << 52; return 0; } while (n--) { cin >> lp; v.push_back(lp); } sort(v.begin(), v.end()); for (auto p : v) { x += p; if (*lower_bound(v.begin(), v.end(), x) == x) { fx = true; px = x; break; } x = xp; } for (auto p : v) { y += p; if (*lower_bound(v.begin(), v.end(), y) == y) { fy = true; py = y; break; } y = yp; } for (auto p : v) { xpy += p; if (*lower_bound(v.begin(), v.end(), xpy) == xpy) { fxpy = true; pxpy = xpy; break; } xpy = xp + yp; } for (auto p : v) { xmy += p; if (*lower_bound(v.begin(), v.end(), xmy) == xmy and (xmy - (xp - yp) - yp > 0 or xmy + yp < l)) { fxmy = true; pxmy = xmy; break; } xmy = xp - yp; } if (fx and fy) { cout << 0; } else if (fx) { cout << 1 << " " << yp; } else if (fy) { cout << 1 << " " << xp; } else if (fxpy) { cout << 1 << " " << xpy - xp; } else if (fxmy) { if (xmy + yp < l) { cout << 1 << " "; cout << xmy + yp; } else if (xmy - (xp - yp) - yp > 0) { cout << 1 << " "; cout << xmy - (xp - yp) - yp; } else { cout << 2 << " " << xp << " " << yp; } } else { cout << 2 << " " << xp << " " << yp; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, l, x, y; int ch[100005]; int findr(int ans); void solve() { int r1 = findr(x), r2 = findr(y), r3 = findr(x + y), r4 = findr(y - x); if (r1 != -1 && r2 != -1) cout << "0" << endl; else if (r1 != -1) cout << "1" << endl << y << endl; else if (r2 != -1) cout << "1" << endl << x << endl; else { if (r4 != -1) cout << "1" << endl << r4 << endl; else if (r3 != -1) cout << "1" << endl << r3 << endl; else if (r3 == -1 && r4 == -1) cout << "2" << endl << x << " " << y << endl; } } int findr(int ans) { int t; for (int i = 0; i < n; i++) { t = lower_bound(ch, ch + n, ch[i] + ans) - ch; if (t == n) break; else { if (ch[t] == ch[i] + ans) { if (ans == y - x) { if (ch[t] - y < 0 && ch[t] + x > l) continue; else if (ch[t] - y >= 0) return ch[t] - y; else return ch[t] + x; } else if (ans == x + y) return ch[t] - y; else return 1; } } } return -1; } int main() { cin >> n >> l >> x >> y; for (int i = 0; i < n; i++) cin >> ch[i]; solve(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int OO = 1e8 + 10; const int MOD = 1e9 + 7; const int MAX = 2e5 + 1; const double EPS = (1e-9); map<int, bool> mp; int main() { int n, x, y, l, last, ans; bool f1 = 0, f2 = 0, f3 = 0; cin >> n >> l >> x >> y; for (int i = 0; i < n; i++) { int a; cin >> a; mp[a] = 1; } for (auto it = mp.begin(); it != mp.end(); it++) { if (mp.count(it->first + (x - y)) && it->first + x <= l) f3 = 1, ans = it->first + x; if (mp.count(it->first - (x - y)) && it->first - x >= 0) f3 = 1, ans = it->first - x; if (mp.count(it->first + x + y)) f3 = 1, ans = it->first + x; if (mp.count(it->first + x)) f1 = 1; if (mp.count(it->first + y)) f2 = 1; } if (f1 && f2) return cout << "0\n", 0; if (f3) return cout << "1\n" << ans << '\n', 0; if (f1) return cout << "1\n" << y << '\n', 0; if (f2) return cout << "1\n" << x << '\n', 0; return cout << "2\n" << x << " " << y << '\n', 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100000; int a[N]; int main() { int n, l, x, y; scanf("%d %d %d %d", &n, &l, &x, &y); for (int i = 0; i < n; ++i) scanf("%d", a + i); bool xok = false, yok = false; for (int i = 0; i < n; ++i) { xok |= binary_search(a, a + n, a[i] + x); yok |= binary_search(a, a + n, a[i] + y); } if (xok && yok) { puts("0"); } else if (xok) { puts("1"); printf("%d\n", y); } else if (yok) { puts("1"); printf("%d\n", x); } else { for (int i = 0; i < n; ++i) { int xx[] = {x, y}; for (int j = 0; j < 2; ++j) { for (int k = -1; k <= 1; k += 2) { int p = a[i] + k * xx[j]; if (p < 0 || p > l) continue; if (binary_search(a, a + n, p + xx[1 - j]) || binary_search(a, a + n, p - xx[1 - j])) { puts("1"); printf("%d\n", p); return 0; } } } } puts("2"); printf("%d %d\n", x, y); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int f(vector<int> &a, int x, bool fl = 0) { int l = 0, r = 0; int ans = 0; while (l < a.size() && r < a.size()) { if (a[r] - a[l] > x) ++l; if (a[r] - a[l] < x) ++r; if (a[r] - a[l] == x && fl == 0) return a[r]; if (fl == 1 && a[r] - a[l] == x) ans = a[r], ++r; } return ans; } int main() { int n, l, x, y; cin >> n >> l >> x >> y; vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; if (f(a, x) && f(a, y)) { cout << 0; return 0; } if (f(a, x)) { cout << 1 << endl; cout << y; return 0; } if (f(a, y)) { cout << 1 << endl; cout << x; return 0; } if (f(a, x + y)) { cout << 1 << endl; cout << f(a, x + y) - x; return 0; } if (f(a, y - x) && f(a, y - x) + x <= l) { cout << 1 << endl; cout << f(a, y - x) + x; return 0; } if (f(a, y - x, 1) && f(a, y - x, 1) - y >= 0) { cout << 1 << endl; cout << f(a, y - x, 1) - y; return 0; } cout << 2 << endl; cout << x << ' ' << y; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, l, x, y, a[100000]; set<int> st; void Solve() { for (int i = 0; i < n; i++) { if (a[i] + y <= l && (a[i] + y <= l - x && st.find(a[i] + x + y) != st.end() || st.find(a[i] + y - x) != st.end())) { printf("1\n%d\n", a[i] + y); return; } if (a[i] - x >= 0 && st.find(a[i] - x + y) != st.end()) { printf("1\n%d\n", a[i] - x); return; } } printf("2\n%d %d\n", x, y); } int main() { scanf("%d%d%d%d", &n, &l, &x, &y); for (int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); for (int i = 0; i < n; i++) st.insert(a[i]); bool fx = false, fy = false; for (int i = 0; i < n && !fx; i++) { if (st.find(a[i] + x) != st.end()) fx = true; } for (int i = 0; i < n && !fy; i++) { if (st.find(a[i] + y) != st.end()) fy = true; } if (fx && fy) printf("0\n"); else if (fx || fy) printf("1\n%d\n", fx ? y : x); else Solve(); getchar(); getchar(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int bj; int cd[222222] = {0}; set<unsigned long long> st; int n, m, x, y; int num; int solve1() { int pos0 = 0, pos1 = 0; for (int i = 1; i <= n; i++) { if (st.find((unsigned long long)cd[i] - (unsigned long long)x) != st.end()) pos0 = 1; if (st.find((unsigned long long)cd[i] - (unsigned long long)y) != st.end()) pos1 = 1; if (pos0 && pos1) return 3; } return (pos0 << 1) + pos1; } int solve2() { if (num) { if (num & 1) return x; return y; } int now; for (int i = 1; i <= n; i++) { now = cd[i] - x; if (now >= 0) { if (st.find((unsigned long long)now + (unsigned long long)y) != st.end()) return now; if (st.find((unsigned long long)now - (unsigned long long)y) != st.end()) return now; } now = cd[i] + x; if (now <= m) { if (st.find((unsigned long long)now + (unsigned long long)y) != st.end()) return now; if (st.find((unsigned long long)now - (unsigned long long)y) != st.end()) return now; } } return -1; } int main() { scanf("%d%d%d%d", &n, &m, &x, &y); for (int i = 1; i <= n; i++) scanf("%d", &cd[i]), st.insert(cd[i]); num = solve1(); if (num == 3) printf("0\n"); else { int k = solve2(); if (k != -1) printf("1\n%d\n", k); else printf("2\n%d %d\n", x, y); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; map<long long, int> mp; map<long long, int>::iterator it; long long l; bool judge(long long x) { for (it = mp.begin(); it != mp.end(); it++) { if (mp.find(it->first + x) == mp.end()) continue; return true; } return false; } bool isin(long long x) { if (x >= 0 && x <= l) return true; return false; } bool chk(long long x, long long y) { for (it = mp.begin(); it != mp.end(); it++) { if (mp.find(it->first + x - y) != mp.end() || mp.find(it->first + x + y) != mp.end()) { if (!isin(it->first + x)) continue; printf("1\n%I64d\n", it->first + x); return true; } if (mp.find(it->first - x + y) != mp.end() || mp.find(it->first - x - y) != mp.end()) { if (!isin(it->first - x)) continue; printf("1\n%I64d\n", it->first - x); return true; } } return false; } int main() { int n; long long x, y, v; cin >> n >> l >> x >> y; for (int i = 1; i <= n; i++) { cin >> v; mp[v] = 1; } int num = 2; if (judge(x)) num--; if (judge(y)) num--; if (num == 0) printf("0\n"); else { if (num == 1) { if (judge(x)) printf("1\n%I64d\n", y); else printf("1\n%I64d\n", x); } else { if (chk(x, y) || chk(y, x)) { } else { printf("2\n%I64d %I64d\n", x, y); } } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; template <typename _type> void outArray(_type a[], long long n) { for (long long i = 1; i <= n; i++) cout << a[i] << " "; cout << endl; } template <typename _type> void outMatrix(_type a[][100005], long long n, long long m) { for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= m; j++) cout << a[i][j] << " "; cout << endl; } } long long n, l; long long a[100005]; long long Search(long long k) { long long i, j, m, t, p, r; long long d; for (i = 1; i <= n - 1; i++) { p = i; r = n; while (p <= r) { m = (p + r) / 2; d = a[m] - a[i]; if (d == k) { return i; } else if (d < k) p = m + 1; else r = m - 1; } } return -1; } long long Search2(long long k, int x) { long long i, j, m, t, p, r; long long d; for (i = 1; i <= n - 1; i++) { p = i; r = n; while (p <= r) { m = (p + r) / 2; d = a[m] - a[i]; if (d == k) { if (a[i] - x > 0) { cout << 1 << endl << a[i] - x; return 1; } else if (a[m] + x < l) { cout << 1 << endl << a[m] + x; return 1; } else break; } else if (d < k) p = m + 1; else r = m - 1; } } return -1; } int main() { long long i, j; long long t, has, c, d, x, y, p, r, g, b, k, k1, sx, sy, dx, dy, dx2, dy2; long long mi, ma; long long c1, c2, c3, c4, c5; cin >> n >> l >> x >> y; for (i = 1; i <= n; i++) cin >> a[i]; b = Search(x); if (b != -1) { b = Search(y); if (b != -1) cout << 0; else cout << 1 << endl << y; } else { b = Search(y); if (b != -1) { cout << 1 << endl << x; } else { b = Search(x + y); if (b != -1) { cout << 1 << endl << a[b] + x; } else { b = Search2(y - x, x); if (b != -1) { } else cout << 2 << endl << x << " " << y; } } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; template <typename A, typename B> inline bool mina(A &x, B y) { return (x > y) ? (x = y, 1) : 0; } template <typename A, typename B> inline bool maxa(A &x, B y) { return (x < y) ? (x = y, 1) : 0; } int N, L, X, Y; int A[(100005)]; int B[(100005)]; int find1(int l) { int i = 0; int j = 0; int c = 0; B[0] = -1; while (j < N) { if (A[j] - A[i] == l) B[c++] = A[i]; if (A[j] - A[i] < l) ++j; else ++i; } return c; } int main() { cin >> N >> L >> X >> Y; for (int _b = (N), i = (0); i < _b; ++i) scanf("%d", A + i); find1(X); int x = B[0]; find1(Y); int y = B[0]; if (x >= 0 && y >= 0) printf("0\n"); else if (x >= 0 && y == -1) printf("1\n%d\n", Y); else if (x == -1 && y >= 0) printf("1\n%d\n", X); else { find1(X + Y); int a = B[0]; if (a != -1) printf("1\n%d\n", a + X); else { int d = Y - X; int bn = find1(d); int f = 0; for (int _b = (bn), i = (0); i < _b; ++i) { int p1 = B[i] - X; if (p1 >= 0) { printf("1\n%d\n", p1); f = 1; break; } int p2 = B[i] + Y; if (p2 <= L) { printf("1\n%d\n", p2); f = 1; break; } } if (!f) printf("2\n%d %d\n", X, Y); } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; map<int, int> M; int A[100005]; int main() { int n, x, y, l; cin >> n >> l >> x >> y; bool can_x = false, can_y = false; for (int i = 0; i < n; i++) { scanf("%d", &A[i]); M[A[i]] = i + 1; } for (int i = 0; i < n; i++) { if (A[i] < x) continue; if (M[A[i] - x] > 0) { can_x = true; break; } } for (int i = 0; i < n; i++) { if (A[i] < y) continue; if (M[A[i] - y] > 0) { can_y = true; break; } } if (can_x && can_y) { cout << 0 << endl; return 0; } if (can_x == true && can_y == false) { cout << 1 << endl; cout << y << endl; return 0; } if (can_x == false && can_y == true) { cout << 1 << endl; cout << x << endl; return 0; } for (int i = 0; i < n; i++) { if (A[i] + x <= l) { if (A[i] + x - y >= 0 && M[A[i] + x - y] > 0) { cout << 1 << endl; cout << A[i] + x << endl; return 0; } if (A[i] + x + y <= l && M[A[i] + x + y] > 0) { cout << 1 << endl; cout << A[i] + x << endl; return 0; } } if (A[i] - x >= 0) { if (A[i] - x - y >= 0 && M[A[i] - x - y] > 0) { cout << 1 << endl; cout << A[i] - x << endl; return 0; } if (A[i] - x + y <= l && M[A[i] - x + y] > 0) { cout << 1 << endl; cout << A[i] - x << endl; return 0; } } } cout << 2 << endl; cout << x << ' ' << y << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const long long inf = 1e18; const long long mod = 1e9 + 7; const long long MOD = 998244353; const double EPS = 1e-8; const double PI = acos(-1.0); long long powMod(long long n, long long p, long long m) { long long res = 1; n %= m; while (p > 0) { if (p & 1) res = (res * n) % m; p = p >> 1; n = (n * n) % m; } return res; } const int N = 2e5 + 5; long long fact[N]; long long invFact[N]; long long fast_pow(long long a, long long p) { long long res = 1; while (p) { if (p % 2 == 0) { a = (a * a) % mod; p /= 2; } else { res = (res * a) % mod; p--; } } return res; } void precalc() { fact[0] = invFact[0] = 1; for (int i = 1; i < N; i++) { fact[i] = (fact[i - 1] * i) % mod; invFact[i] = fast_pow(fact[i], mod - 2); } } long long C(int n, int k) { if (k > n) { return 0; } return fact[n] * invFact[k] % mod * invFact[n - k] % mod; } long long sumdigit(long long n) { long long sum = 0; while (n) { sum += n % 10; n /= 10; } return sum; } void solve() { long long n, l, x, y; cin >> n >> l >> x >> y; vector<long long> arr(n); for (long long i = 0; i < n; i++) cin >> arr[i]; bool fx = 0, fy = 0, fd = 0; long long d = y - x; set<long long> st; for (long long i = 0; i < n; i++) st.insert(arr[i]); for (auto v : st) { if (st.count((v - x)) || st.count(x + v)) { fx = 1; } if (st.count((v - y)) || st.count(y + v)) { fy = 1; } } if (fx && fy) { cout << "0\n"; return; } if (fx && !fy) { cout << "1\n"; cout << y << '\n'; return; } if (!fx && fy) { cout << "1\n"; cout << x << '\n'; return; } for (auto v : st) { if (st.count(v + x + y)) { if (st.count(v + x)) { cout << 0 << '\n'; return; } cout << 1 << '\n'; cout << v + x << '\n'; return; } if (st.count(v + d)) { if (v - x < 0 && v + y > l) continue; cout << "1\n"; if (v - x >= 0) cout << v - x << '\n'; else if (v + y <= l) cout << v + y << '\n'; return; } } cout << "2\n"; cout << x << " " << y << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) solve(); return 0; }
8
CPP
a = input().split(' ') b = input().split(' ') n=int(a[1]) x=int(a[2]) y=int(a[3]) xcan=False ycan=False xycan=False xymoar=False cool=0 cool2=0 s=set() for element in b: s.add(int(element)) for c in s: if c+x in s: xcan=True if c+y in s: ycan=True if c+x+y in s: xycan = True cool=c if c+y-x in s: if c+y>n: if c-x<0: pass else: cool2 = c-x xymoar=True else: xymoar = True cool2 = c+y if xcan: if ycan: break if xcan==True: if ycan==True: result=0 marks=[] else: result = 1 marks=[y] else: if ycan == True: result = 1 marks=[x] else: if xycan == True: result=1 marks=[cool+x] elif xymoar == True: result=1 marks=[cool2] else: result=2 marks=[x,y] print(result) for i in range(len(marks)): print(marks[i], end=' ')
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, l, x, y; cin >> n >> l >> x >> y; multiset<int> s; for (int i = 0; i < n; i++) { long long int a; cin >> a; s.insert(a); } bool x_found = false, y_found = false; for (long long int el : s) { if (s.find(el + x) != s.end()) x_found = true; if (s.find(el + y) != s.end()) y_found = true; } if (x_found && y_found) cout << 0 << '\n'; else if (x_found || y_found) { if (!x_found) cout << 1 << '\n' << x << '\n'; else cout << 1 << '\n' << y << '\n'; } else { bool one = false; for (int el : s) { if ((el + x <= l) && (s.find(el + x + y) != s.end() || s.find(el + x - y) != s.end())) { cout << 1 << '\n'; cout << el + x << '\n'; one = true; break; } if ((el - x >= 0) && (s.find(el - x - y) != s.end() || s.find(el - x + y) != s.end())) { cout << 1 << '\n'; cout << el - x << '\n'; one = true; break; } } if (!one) cout << 2 << '\n' << x << " " << y << '\n'; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; long long MOD_EXPO(long long b, long long p, long long m) { if (p == 0) return 1; long long ret = MOD_EXPO(b, p / 2, m) % m; ret = (ret * ret) % m; return ((p & 1) ? (ret * b) % m : ret % m); } long long POWER(long long N, long long K) { long long i, ans = 1; for (i = 1; i <= K; i++) ans *= N; return ans; } int SET(int N, int pos) { return (N | (1 << pos)); } int RESET(int N, int pos) { return (N & !(1 << pos)); } bool CHECK(int N, int pos) { return (N & (1 << pos)); } int dx4[] = {1, -1, 0, 0}; int dy4[] = {0, 0, 1, -1}; int dx6[] = {0, 0, 1, -1, 0, 0}; int dy6[] = {1, -1, 0, 0, 0, 0}; int dz6[] = {0, 0, 0, 0, 1, -1}; int dx8[] = {1, -1, 0, 0, -1, 1, -1, 1}; int dy8[] = {0, 0, 1, -1, 1, 1, -1, -1}; int dkx8[] = {-1, 1, -1, 1, -2, -2, 2, 2}; int dky8[] = {2, 2, -2, -2, 1, -1, 1, -1}; int tc = 1; const double eps = 1e-9; const double pi = acos(-1.0); const long long int mx = 1e5; const long long int mod = 1e9 + 7; long long arr[mx + 5]; int main() { long long n, l, x, y, diff, i, a, b, c, d, e, sum, bal; bool xflag, yflag, dflag, bflag, cflag; while (cin >> n >> l >> x >> y) { map<long long, bool> mp; vector<long long> idx; for (i = 1; i <= n; i++) { scanf("%lld", &arr[i]); mp[arr[i]] = true; } xflag = yflag = dflag = bflag = cflag = false; for (i = 1; i <= n; i++) { a = x + arr[i]; b = y + arr[i]; c = (y - x) + arr[i]; d = x + y + arr[i]; e = y - x; if (mp[a]) xflag = true; if (mp[b]) yflag = true; if (mp[c] && !dflag && (c - y >= 0 || c + x <= l)) diff = c, dflag = true; if (mp[d] && !bflag && (d - y >= 0 || d + x <= l)) sum = d, bflag = true; if (mp[e] && !cflag && (e - y >= 0 || e + x <= l)) bal = e, cflag = true; } if (!xflag && !yflag) { if (mp[x + y]) idx.push_back(x); else if (cflag && bal + x <= l) idx.push_back(bal + x); else if (bflag && sum - y >= 0) idx.push_back(sum - y); else if (bflag && sum + x <= l) idx.push_back(sum + x); else if (dflag && diff - y >= 0) idx.push_back(diff - y); else if (dflag && diff + x <= l) idx.push_back(diff + x); else idx.push_back(x), idx.push_back(y); } else if (!xflag) { idx.push_back(x); } else if (!yflag) { idx.push_back(y); } cout << idx.size() << "\n"; for (i = 0; i < idx.size(); i++) printf("%lld ", idx[i]); printf("\n"); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int a[100010]; set<int> s; int n, L; int lo, hi, mid; int fin(int tar) { lo = 0; hi = n - 1; while (lo <= hi) { mid = (lo + hi) / 2; if (a[mid] > tar) hi = mid - 1; else lo = mid + 1; } if (hi >= 0 && a[hi] == tar) return 1; return 0; } int get(int val) { int i; for (i = 0; i < n; i++) { if (fin(a[i] + val) || fin(a[i] - val)) return 1; } return 0; } int make(int val, int other_val) { int i, val2; for (i = 0; i < n; i++) { if ((a[i] + val) <= L) { val2 = a[i] + val; if (fin(val2 - other_val) || fin(val2 + other_val)) { printf("1\n"); printf("%d\n", val2); return 1; } } if ((a[i] - val) >= 0) { val2 = a[i] - val; if (fin(val2 - other_val) || fin(val2 + other_val)) { printf("1\n"); printf("%d\n", val2); return 1; } } } return 0; } int main() { int i, j, k, x, y; scanf("%d %d %d %d", &n, &L, &x, &y); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } int gotx = get(x); int goty = get(y); if (gotx && goty) { printf("0\n"); return 0; } if (gotx || goty) { printf("1\n"); if (gotx) printf("%d\n", y); if (goty) printf("%d\n", x); return 0; } int val = x; int other_val = y; int sts = make(val, other_val); if (sts) return 0; sts = make(other_val, val); if (sts) return 0; printf("2\n"); printf("%d %d\n", x, y); return 0; }
8
CPP
if __name__ == "__main__": n, l, x, y = list(map(int, input().split())) v = list(map(int, input().split())) s = set(v) cx = 0 for i in range(n): if v[i]+x in s: cx = 1 break cy = 0 for i in range(n): if v[i]+y in s: cy = 1 break count = 0 ans = [] if cx==0: count += 1 ans.append(x) if cy==0: count += 1 ans.append(y) if count==2: for i in range(n): if (v[i]+x+y in s): count = 1 ans = [v[i]+x] break if count==2: for i in range(n): if (v[i]+x-y in s): if v[i]+x<=l: count = 1 ans = [v[i]+x] break elif v[i]-y>=0: count = 1 ans = [v[i]-y] break print(count) if count!=0: print(*ans)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using pi = pair<int, int>; int n, l, x, y; unordered_set<int> locs; bool valid(int x) { return x >= 0 && x <= l; } bool dfx = false, dfy = false; bool ok(int p) { bool xok = dfx, yok = dfy; if (valid(p + x) && locs.count(p + x)) { xok = true; } else if (valid(p - x) && locs.count(p - x)) { xok = true; } if (valid(p + y) && locs.count(p + y)) { yok = true; } else if (valid(p - y) && locs.count(p - y)) { yok = true; } return xok && yok; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> l >> x >> y; for (int i = 0; i < n; i++) { int cx; cin >> cx; locs.insert(cx); if (valid(cx - x) && locs.count(cx - x)) dfx = true; if (valid(cx - y) && locs.count(cx - y)) dfy = true; } int ans = 2; pi ret = make_pair(x, y); for (int v : locs) { if (ok(v)) { ans = 0; break; } if (valid(v + x) && ok(v + x)) { ans = min(ans, 1); ret.first = v + x; } if (valid(v - x) && ok(v - x)) { ans = min(ans, 1); ret.first = v - x; } if (valid(v + y) && ok(v + y)) { ans = min(ans, 1); ret.first = v + y; } if (valid(v - y) && ok(v - y)) { ans = min(ans, 1); ret.first = v - y; } } cout << ans << '\n'; if (ans == 2) { cout << ret.first << " " << ret.second << '\n'; } else if (ans == 1) { cout << ret.first << '\n'; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { map<long long, int> num; num.clear(); long long n, l, x, y, curr; cin >> n >> l >> x >> y; int boy = 0, girl = 0; long long diff = y - x, sum = y + x, ans2 = -1, ans3 = -1; for (int i = 0; i <= n - 1; i++) { cin >> curr; if (boy & girl) continue; num[curr] = 1; if (curr >= x && num[curr - x]) girl = 1; if (curr >= y && num[curr - y]) boy = 1; if (ans2 == -1 && ans3 == -1) { if (curr >= diff && num[curr - diff]) { if (curr + x <= l) ans2 = curr + x; else if (curr - y >= 0) ans2 = curr - y; } if (curr >= sum && num[curr - sum]) ans3 = curr - sum; } } int ans = 2; if (boy) ans--; if (girl) ans--; if (ans == 0) { cout << ans << endl; return 0; } else if (ans == 1) { cout << "1\n"; if (!boy) { cout << y; return 0; } else if (!girl) { cout << x; return 0; } } else { if (ans2 != -1) { cout << "1\n"; cout << ans2; return 0; } if (ans3 != -1) { if (ans3 + x <= l) { cout << "1\n"; cout << ans3 + x; return 0; } } cout << "2\n"; cout << x << " " << y << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, l, x, y, a[100005]; map<int, bool> ma; void ck(int d, int t) { if (d < 0 || d > l) { return; } if (ma[d - t] || ma[d + t]) { cout << 1 << endl << d; exit(0); } } int main() { ios::sync_with_stdio(false); cin >> n >> l >> x >> y; for (int i = 0; i < n; i++) { cin >> a[i]; ma[a[i]] = true; } bool xok = false, yok = false; for (int i = 0; i < n; i++) { if (ma[a[i] + x] || ma[a[i] - x]) xok = true; if (ma[a[i] + y] || ma[a[i] - y]) yok = true; } if (xok && yok) { cout << 0; return 0; } else if (xok) { cout << 1 << endl << y; return 0; } else if (yok) { cout << 1 << endl << x; return 0; } else { for (int i = 0; i < n; i++) { ck(a[i] + x, y); ck(a[i] - x, y); ck(a[i] + y, x); ck(a[i] - y, x); } cout << 2 << endl << x << ' ' << y; return 0; } }
8
CPP
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9, maxn = (int)1e5 + 1; const double eps = (double)1e-8; const int mod = (int)1000000009; map<int, bool> m; int a[100001], n, l, x, y, z, one; bool tx = 0, ty = 0, t1 = 0; int main() { cin >> n >> l >> x >> y; for (int i = (1); i <= (n); i++) { scanf("%d", &(a[i])); m[a[i]] = 1; } for (int i = 1; i <= n; i++) { z = a[i]; if (m[z - x] == 1 || m[z + x] == 1) tx = 1; else { if (z - x >= 0 && z - x <= l && (m[z - x - y] == 1 || m[y + (z - x)] == 1)) { one = z - x; t1 = 1; } else if (x + z >= 0 && x + z <= l && (m[x + z - y] == 1 || m[y + (x + z)] == 1)) { one = x + z; t1 = 1; } } if (m[z - y] == 1 || m[y + z] == 1) ty = 1; else { if (z - y >= 0 && z - y <= l && (m[z - y - x] == 1 || m[x + (z - y)] == 1)) { one = z - y; t1 = 1; } if (y + z >= 0 && y + z <= l && (m[y + z - x] == 1 || m[x + (y + z)] == 1)) { one = y + z; t1 = 1; } } } if (tx == 1 && ty == 1) { cout << 0; } else if (t1 == 1) { cout << 1 << endl; cout << one; } else if (tx == 1) { cout << 1 << endl; cout << y; } else if (ty == 1) { cout << 1 << endl; cout << x; } else { cout << 2 << endl; cout << x << " " << y; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; int main() { int n, l, x, y; int ans = 0; cin >> n >> l >> x >> y; std::vector<int> a; int z; for (int i = 0; i < n; i++) { cin >> z; a.push_back(z); } std::vector<int> gm, gf; std::vector<int> cm, cf; for (int i = 0; i < n; i++) { cm.push_back(a[i]); cm.push_back(a[i] + y); cf.push_back(a[i]); cf.push_back(a[i] + x); } sort((cm).begin(), (cm).end()); sort((cf).begin(), (cf).end()); bool m = false; bool f = false; for (int i = 1; i < 2 * n; i++) if (cm[i] == cm[i - 1]) m = true; for (int i = 1; i < 2 * n; i++) if (cf[i] == cf[i - 1]) f = true; if (m && f) { cout << 0; return 0; } if (m || f) { cout << 1 << endl; if (m) cout << x; else cout << y; return 0; } for (int i = 0; i < n; i++) { if (a[i] >= y) gm.push_back(a[i] - y); if (a[i] + y <= l) gm.push_back(a[i] + y); if (a[i] >= x) gf.push_back(a[i] - x); if (a[i] + x <= l) gf.push_back(a[i] + x); } sort((gf).begin(), (gf).end()); sort((gm).begin(), (gm).end()); int fs = gf.size(); int ms = gm.size(); int curf = 0, curm = 0; while ((curf < fs) && (curm < ms)) { if (gf[curf] == gm[curm]) { cout << 1 << endl << gf[curf]; return 0; } else if (gf[curf] > gm[curm]) { curm++; } else curf++; } cout << 2 << endl; cout << x << " " << y; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long int n, l, x, y; cin >> n >> l >> x >> y; long long int arr[100005]; for (int i = 0, _i = (n); i < _i; ++i) cin >> arr[i]; bool possi1 = false, possi2 = false; for (int i = 0, _i = (n - 1); i < _i; ++i) { if (arr[lower_bound(arr + i, arr + n, arr[i] + x) - arr] == arr[i] + x) possi1 = true; if (arr[lower_bound(arr + i, arr + n, arr[i] + y) - arr] == arr[i] + y) possi2 = true; } if (possi1 && possi2) cout << 0 << endl; else { if (!possi2 && !possi1) { long long int temp = x + y; int index; bool po = false; for (int i = 0, _i = (n - 1); i < _i; ++i) { if (arr[lower_bound(arr + i, arr + n, arr[i] + temp) - arr] == temp + arr[i]) { po = true; index = i; } } if (po) { cout << 1 << endl; cout << arr[index] + x; return 0; } temp = y - x; for (int i = 0, _i = (n - 1); i < _i; ++i) { if (arr[lower_bound(arr + i, arr + n, arr[i] + temp) - arr] == temp + arr[i]) { if (arr[i] >= x) { cout << 1 << endl; cout << arr[i] - x << endl; return 0; } else if (arr[i] + y <= l) { cout << 1 << endl; cout << arr[i] + y << endl; return 0; } } } cout << 2 << endl; cout << x << " " << y << endl; } else { cout << 1 << endl; if (possi2) cout << x << endl; else cout << y << endl; } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, l, x, y; cin >> n >> l >> x >> y; std::vector<long long> v(n, 0); for (int i = 0; i < n; ++i) { cin >> v[i]; } long long a = 0, b = 0; for (int i = 0; i < n; ++i) { if (binary_search(v.begin(), v.end(), v[i] + x)) a++; if (binary_search(v.begin(), v.end(), v[i] + y)) b++; } if (a && b) { cout << 0; return 0; } if (a) { cout << 1 << '\n' << y; return 0; } if (b) { cout << 1 << '\n' << x; return 0; } b = 0; for (int i = 0; i < n && (v[i] + x <= l); ++i) { if (binary_search(v.begin(), v.end(), v[i] + x - y) || binary_search(v.begin(), v.end(), v[i] + x + y)) { cout << 1 << '\n' << v[i] + x; return 0; } } for (int i = n - 1; i >= 0 && (v[i] - x >= 0); --i) { if (binary_search(v.begin(), v.end(), v[i] - x - y) || binary_search(v.begin(), v.end(), v[i] - x + y)) { cout << 1 << '\n' << v[i] - x; return 0; } } cout << 2 << '\n' << x << " " << y; }
8
CPP
#include <bits/stdc++.h> using namespace std; set<int> marks; int l; int x, y; int a[200200]; int n; int main() { cin >> n >> l >> x >> y; for (int i = 0; i < n; ++i) { cin >> a[i]; marks.insert(a[i]); } bool xx = false; bool yy = false; for (int i = 0; i < n; ++i) { int start = a[i]; int end = a[i] + x; if (marks.find(end) != marks.end()) { xx = true; break; } } for (int i = 0; i < n; ++i) { int start = a[i]; int end = a[i] + y; if (marks.find(end) != marks.end()) { yy = true; break; } } if (xx && yy) { cout << 0 << endl; return 0; } if (xx) { cout << 1 << endl << y << endl; return 0; } if (yy) { cout << 1 << endl << x << endl; return 0; } for (int i = 0; i < n; ++i) { int start = a[i]; int end1 = a[i] + y - x; int end2 = a[i] + x + y; if (marks.find(end2) != marks.end()) { cout << 1 << endl << start + x << endl; return 0; } if (marks.find(end1) != marks.end()) { if (start - x >= 0) { cout << 1 << endl << start - x << endl; return 0; } if (end1 + x <= l) { cout << 1 << endl << end1 + x << endl; return 0; } } } cout << 2 << endl << x << ' ' << y << endl; return 0; }
8
CPP
#Adapted from code of hatsuyuki15 in this contest n, l, x, y = map(int, input().split()) a = set(map(int, input().split())) boy = False girl = False one = False where = -1 for i in a: if i + x in a: boy = True if i + y in a: girl = True if i - x > 0 and i - x + y in a: one = True where = i - x if i + x < l and i + x - y in a: one = True where = i + x if i + x + y in a: one = True where = i + x if i - x - y in a: one = True where = i - x if boy and girl: print(0) if boy and not girl: print(1) print(y) if girl and not boy: print(1) print(x) if not boy and not girl: if one: print(1) print(where) if not one: print(2) print(x, y)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 100050; int n, l, x, y, a[maxn]; map<int, int> q; int main() { int i, j; bool z1 = 0, z2 = 0; scanf("%d%d%d%d\n", &n, &l, &x, &y); for (i = 1; i <= n; i++) { scanf("%d", &a[i]); q[a[i]] = 1; if (q.count(a[i] - x)) z1 = 1; if (q.count(a[i] - y)) z2 = 1; } if (z1 && z2) printf("0\n"); else if (z1) printf("1\n%d\n", y); else if (z2) printf("1\n%d\n", x); else { for (i = 1; i <= n; i++) if (q[a[i] + x - y]) { if (a[i] + x <= l) { printf("1\n%d\n", a[i] + x); return 0; } if (a[i] - y >= 0) { printf("1\n%d\n", a[i] - y); return 0; } } else if (q[a[i] - x - y]) { printf("1\n%d\n", a[i] - x); return 0; } printf("2\n%d %d\n", x, y); } return 0; }
8
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1000000000") using namespace std; const int maxn = 100010; int v[maxn]; int main() { int n, l, x, y; scanf("%d %d %d %d", &n, &l, &x, &y); for (int i = 0; i < n; i++) { scanf("%d", &v[i]); } v[n] = (int)2e9 + 10; bool f = false, s = false; for (int i = 0; i < n; i++) { int need = v[i] - x; if ((*lower_bound(v, v + n, need)) == need) { f = true; } need = v[i] - y; if ((*lower_bound(v, v + n, need)) == need) { s = true; } } if (f && s) { cout << 0 << endl; return 0; } if (!f && s) { cout << 1 << endl; cout << x << endl; return 0; } if (f && !s) { cout << 1 << endl; cout << y << endl; return 0; } for (int i = 0; i < n; i++) { int need = v[i] + x; if (need <= l) { if ((*lower_bound(v, v + n, need - y)) == need - y || (*lower_bound(v, v + n, need + y)) == need + y) { cout << 1 << endl; cout << need << endl; return 0; } } need = v[i] - x; if (need >= 0) { if ((*lower_bound(v, v + n, need - y)) == need - y || (*lower_bound(v, v + n, need + y)) == need + y) { cout << 1 << endl; cout << need << endl; return 0; } } } cout << 2 << endl; cout << x << ' ' << y << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, L, x, y; while (scanf("%d", &n) == 1) { scanf("%d", &L), scanf("%d", &x), scanf("%d", &y); vector<int> v(n); set<int> SET; for (int i = 0; i < n; ++i) { int val; scanf("%d", &val); v[i] = val; SET.insert(val); } bool foundX = 0, foundY = 0; for (int i = 0; i < n; ++i) if (SET.count(v[i] - x) || SET.count(v[i] + x)) { foundX = 1; break; } for (int i = 0; i < n; ++i) if (SET.count(v[i] - y) || SET.count(v[i] + y)) { foundY = 1; break; } if (foundX && foundY) { puts("0"); continue; } if (foundX) { puts("1"); printf("%d\n", y); continue; } if (foundY) { puts("1"); printf("%d\n", x); continue; } bool found = 0; for (int i = 0; i < n; ++i) { int target = v[i] - x; if (target >= 0 && target <= L && (SET.count(target - y) || SET.count(target + y))) { found = 1; puts("1"); printf("%d\n", target); break; } target = v[i] + x; if (target >= 0 && target <= L && (SET.count(target - y) || SET.count(target + y))) { found = 1; puts("1"); printf("%d\n", target); break; } } if (found) continue; puts("2"); printf("%d %d\n", x, y); } }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, l, x, y, b1, b2, o, ans, tot, a[200010]; map<int, int> b, bb; int main() { scanf("%d%d%d%d", &n, &l, &x, &y); for (int i = 1; i <= n; i++) { scanf("%d", &o); b[x + o] = 1; bb[y + o] = 1; bb[o - y] = 1; b[o - x] = 1; if (b[o]) b1 = 1; a[++tot] = x + o; a[++tot] = o - x; if (bb[o]) b2 = 1; } if (b1 && b2) return puts("0"), 0; for (int i = 1; i <= tot; i++) if (bb[a[i]] && a[i] <= l && a[i] >= 0) { ans = a[i]; break; } if (ans) { puts("1"); printf("%d", ans); return 0; } printf("%d\n", (b1 ^ 1) + (b2 ^ 1)); if (!b1) printf("%d ", x); if (!b2) printf("%d", y); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; bool isFile = 0; const long long INF = 1e15 + 1234; const int N = 1e5 + 1234; const long long MOD = 998244353; const long long mod = 1e9 + 7; double EPS = 3e-18; const double PI = acos(-1); void faster() { if (isFile) { freopen( "sort" ".in", "r", stdin); freopen( "sort" ".out", "w", stdout); } ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } long long n, l, x, y; long long a[N]; bool fl = 0, f = 0; set<int> ans; map<long long, bool> m; bool check(long long r) { for (int i = 1; i <= n; i++) { int c = 0, k = i; while (c + 1 < k) { int m = (c + k) / 2; if (a[i] - a[m] < r) k = m; else c = m; } if (a[i] - a[c] == r) { return true; } c = i; k = n + 1; while (c + 1 < k) { int m = (c + k) / 2; if (a[m] - a[i] <= r) c = m; else k = m; } if (a[c] - a[i] == r) { return true; } } return false; } void solve() { cin >> n >> l >> x >> y; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] == x) fl = 1; if (a[i] == y) f = 1; m[a[i]] = 1; } if (!fl) fl = check(x); if (!f) f = check(y); pair<int, int> cc = {-1, -1}; if (!f && !fl) { long long r = x + y; for (int i = 1; i <= n; i++) { int c = 0, k = i; while (c + 1 < k) { int m = (c + k) / 2; if (a[i] - a[m] < r) k = m; else c = m; } if (a[i] - a[c] == r) { if (m.count(a[c] + x) || m.count(a[c] + y)) { cc.first = 0; break; } else { cc.first = i; cc.second = c; } } c = i; k = n + 1; while (c + 1 < k) { int m = (c + k) / 2; if (a[m] - a[i] <= r) c = m; else k = m; } if (a[c] - a[i] == r) { if (m.count(a[c] - x) || m.count(a[c] - y)) { cc.first = 0; break; } else { cc.first = i; cc.second = c; } } } } pair<int, int> dd = {-1, -1}; if (!f && !fl) { long long r = y - x; for (int i = 1; i <= n; i++) { int c = 0, k = i; while (c + 1 < k) { int m = (c + k) / 2; if (a[i] - a[m] < r) k = m; else c = m; } if (a[i] - a[c] == r) { if (m.count(a[c] + y)) { dd.first = 0; break; } else if (a[c] + y <= l) { dd.first = i; dd.second = c; } } c = i; k = n + 1; while (c + 1 < k) { int m = (c + k) / 2; if (a[m] - a[i] <= r) c = m; else k = m; } if (a[c] - a[i] == r) { if (m.count(a[c] - y)) { dd.first = 0; break; } else if (a[c] - y >= 0) { dd.first = i; dd.second = c; } } } } if (cc.first == 0 || dd.first == 0) { cout << 0; return; } else if (cc.first > 0) { cout << "1\n"; if (cc.second > cc.first) { cout << (a[cc.second] - y); } else cout << (a[cc.second] + y); return; } else if (dd.first > 0) { cout << "1\n"; if (dd.second > dd.first) { cout << (a[dd.second] - y); } else cout << (a[dd.second] + y); return; } if (!fl) ans.insert(x); if (!f) ans.insert(y); cout << (long long)ans.size(); cout << "\n"; for (auto i : ans) cout << i << ' '; } int main() { faster(); int t = 1; while (t--) { solve(); cout << "\n"; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:100000000") int mas[100100]; int sum[100100]; set<int> st; int main() { int n, l, x, y; scanf("%d %d %d %d", &n, &l, &x, &y); for (int i = 0; i < n; i++) scanf("%d", &mas[i]); for (int i = 0; i < n; i++) st.insert(mas[i]); bool f1 = false; bool f2 = false; for (int i = 0; i < n; i++) { int v = mas[i]; if (st.find(v + x) != st.end()) f1 = true; if (st.find(v + y) != st.end()) f2 = true; } if (f1 && f2) { printf("0\n"); return 0; } if (f1 || f2) { printf("1\n"); if (f1) printf("%d\n", y); else printf("%d\n", x); return 0; } bool f = false; for (int i = 0; i < n; i++) { int v = mas[i]; int lll = v - x; int rr = v + x; if (lll >= 0) { if (st.find(lll + y) != st.end() || st.find(lll - y) != st.end()) { printf("1\n%d\n", lll); return 0; } } if (rr <= l) { if (st.find(rr + y) != st.end() || st.find(rr - y) != st.end()) { printf("1\n%d\n", rr); return 0; } } } printf("2\n%d %d", x, y); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; void in() {} map<int, int> m; int n, l, x, y, a[200100], cnt = 2; int main() { in(); cin >> n >> l >> x >> y; if (x > y) swap(x, y); for (int i = 0; i < n; ++i) { scanf("%d", a + i); m[a[i]]++; } for (int i = 0; i < n; ++i) { if (x > -1 && m[a[i] + x] > 0) { x = -1; cnt--; } if (y > -1 && m[a[i] + y] > 0) { y = -1; cnt--; } if (cnt == 0) { cout << 0 << endl; return 0; } } if (cnt == 2) { for (int i = 0; i < n; ++i) { if (m[a[i] + x + y] > 0) { cout << 1 << endl << a[i] + x << endl; return 0; } if (m[a[i] + x - y] > 0 && a[i] + x < l) { cout << 1 << endl << a[i] + x << endl; return 0; } if (m[a[i] - x + y] > 0 && a[i] - x > 0) { cout << 1 << endl << a[i] - x << endl; return 0; } if (m[a[i] - x - y] > 0) { cout << 1 << endl << a[i] - x << endl; return 0; } } } cout << cnt << endl; if (x != -1) cout << x << " "; if (y != -1) cout << y; cout << endl; return 0; }
8
CPP
def build_graph(): line1 = input().strip().split() n = int(line1[0]) m = int(line1[1]) graph = {} for _ in range(m): line = input().strip().split() u = int(line[0]) v = int(line[1]) c = int(line[2]) if c not in graph: graph[c] = {j: [] for j in range(1, n+1)} graph[c][u].append(v) graph[c][v].append(u) return graph def no_of_paths(u, v, graph): x = 0 for c in graph: parent = {} parent = dfs_visit(v, graph[c], parent) if u in parent: x += 1 return x def dfs_visit(i, adj_list, parent): for j in adj_list[i]: if j not in parent: parent[j] = i dfs_visit(j, adj_list, parent) return parent if __name__ == "__main__": graph = build_graph() for _ in range(int(input())): line = input().strip().split() print(no_of_paths(int(line[0]), int(line[1]), graph))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; long long int power(long long int x, long long int y) { long long int res = 1; x = x; while (y > 0) { if (y & 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } long long int logtwo(long long int n) { if (n == 1) return 0; return logtwo(n / 2) + 1; } vector<long long int> adj[105][105]; long long int vis[105][105]; void dfs(int par, long long int col) { for (auto child : adj[par][col]) { if (vis[child][col]) continue; vis[child][col] = vis[par][col]; dfs(child, col); } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; while (t--) { long long int N, M; cin >> N >> M; for (long long int i = 0; i < M; i++) { long long int a, b, c; cin >> a >> b >> c; a--; b--; adj[a][c].push_back(b); adj[b][c].push_back(a); } for (long long int i = 0; i < 105; i++) { long long int c = 1; for (long long int j = 0; j < N; j++) { if (vis[j][i]) continue; vis[j][i] = c++; dfs(j, i); } } long long int Q; cin >> Q; while (Q--) { long long int x, y; cin >> x >> y; long long int ans = 0; x--; y--; for (long long int i = 0; i < 105; i++) { if (vis[x][i] && vis[x][i] == vis[y][i]) ans++; } cout << ans << "\n"; } } cerr << "Time : " << 1000 * (long double)clock() / (long double)CLOCKS_PER_SEC << "ms\n"; ; }
8
CPP
#include <bits/stdc++.h> const int N = 1e5 + 100, M = 1e6 + 100, SQ = sqrt(2e5), LG = 23, base = 2, second = 1e2 + 100; const long long mod = 1e9 + 7, MOD = 1e9 + 9, Inf = 9223372036854775807; const long long INF = 1e9, inf = 1e18, super_inf = ~0ull / 4; const long double Pi = (22 * 1.0) / (7 * 1.0); using namespace std; long long n, m, ans, a, b, c, q; vector<pair<long long, long long> > v[second]; bool mark[second]; void dfs(long long a, long long b, int c) { mark[a] = 1; for (auto x : v[a]) { if (!mark[x.first] and x.second == c) dfs(x.first, b, c); } } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); ; cin >> n >> m; for (int i = 0; i < m; i++) cin >> a >> b >> c, v[a].push_back(make_pair(b, c)), v[b].push_back(make_pair(a, c)); cin >> q; while (q--) { cin >> a >> b; ans = 0; for (int i = 1; i <= m; i++) { fill(mark, mark + second, 0); dfs(a, b, i); if (mark[b]) ans++; } cout << ans << endl; } }
8
CPP
# !/usr/bin/env python3 # coding: UTF-8 # Modified: <19/Feb/2019 06:31:16 PM> # ✪ H4WK3yE乡 # Mohd. Farhan Tahir # Indian Institute Of Information Technology (IIIT),Gwalior # Question Link # # # ///==========Libraries, Constants and Functions=============/// import sys inf = float("inf") mod = 1000000007 def get_array(): return list(map(int, sys.stdin.readline().split())) def get_ints(): return map(int, sys.stdin.readline().split()) def input(): return sys.stdin.readline() # ///==========MAIN=============/// N = 105 graph = [[] for _ in range(N)] component = [0] * N visited = [False] * N #cnt = 0 #v = 0 #n, m = 0, 0 #ans = 0 def explore(node): global cnt visited[node] = True component[node] = cnt for neighbour, color in graph[node]: if visited[neighbour] == False: explore(neighbour) def dfs(n): global cnt cnt = 0 for i in range(1, n + 1): if visited[i] == False: cnt += 1 explore(i) def explore_o(node, c): global ans, s visited[node] = True for neighbour, color in graph[node]: if visited[neighbour] == False and color == c: if neighbour == v: s.add(color) explore_o(neighbour, c) def dfs_o(u): global visited, ans, flag, s ans = 0 s = set() for neighbour, color in graph[u]: visited = [False] * N visited[u] = True flag = 0 if neighbour == v: s.add(color) continue explore_o(neighbour, color) return len(s) def main(): global n, m n, m = get_ints() for _ in range(m): u, x, c = get_ints() graph[u].append((x, c)) graph[x].append((u, c)) dfs(n) global v for tc in range(int(input())): u, v = get_ints() if component[u] != component[v]: print(0) continue print(dfs_o(u)) if __name__ == "__main__": main()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-8; const int mxn = 233; const int mod = 1e9 + 7; inline int read() { int x = 0, f = 1; char c = getchar(); while (!isdigit(c)) f = c == '-' ? -1 : 1, c = getchar(); while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); return x * f; } inline long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long ksm(long long a, long long b, long long mod) { int ans = 1; while (b) { if (b & 1) ans = (ans * a) % mod; a = (a * a) % mod; b >>= 1; } return ans; } long long inv2(long long a, long long mod) { return ksm(a, mod - 2, mod); } void exgcd(long long a, long long b, long long &x, long long &y, long long &d) { if (!b) { d = a; x = 1; y = 0; } else { exgcd(b, a % b, y, x, d); y -= x * (a / b); } } int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; int n, m, d[mxn][mxn][mxn]; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); ; cin >> n >> m; int u, v, c; for (int i = 1; i <= m; ++i) { cin >> u >> v >> c; d[c][v][u] = d[c][u][v] = 1; } for (int c = 1; c <= m; ++c) for (int k = 1; k <= n; ++k) for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) d[c][i][j] = d[c][j][i] = max(d[c][i][j], d[c][i][k] & d[c][k][j]); int q; cin >> q; while (q--) { cin >> u >> v; int res = 0; for (int i = 1; i <= m; ++i) if (d[i][u][v]) res++; cout << res << endl; } return 0; }
8
CPP
def dfs(start , color): for i in g[start]: if not vis[i[0]] and i[1] == color : vis[i[0]] = True dfs(i[0] , i[1]) n , m = map(int,input().split()) g = [[] for i in range(n)] for i in range(m): a , b , c = map(int,input().split()) g[a - 1].append((b - 1, c)) g[b-1].append((a-1 , c)) q = int(input()) res = [0]*q for i in range(q): u , v = map(int,input().split()) for j in range(1 , m + 1): vis = [False] * n dfs(u - 1 , j) if vis[v - 1]: res[i] +=1 #print(res) for i in res: print(i)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; bool visited[100], colors[100]; vector<vector<pair<int, int>>> adj_list(100); unordered_map<int, bool> M; int n, m, a, b, c; bool dfs(int start, int end, int color = -1) { if (visited[start]) return 0; if (start == end) return 1; visited[start] = 1; int len = adj_list[start].size(); int ans = 0; for (int i = 0; i < len; i++) { if (color == -1 || color == adj_list[start][i].second) if (dfs(adj_list[start][i].first, end, adj_list[start][i].second)) M[adj_list[start][i].second] = 1; } visited[start] = 0; return 0; } int main() { memset(visited, 0, sizeof(visited)); memset(colors, 0, sizeof(colors)); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> a >> b >> c; a--; b--; c--; adj_list[a].push_back({b, c}); adj_list[b].push_back({a, c}); } cin >> c; while (c--) { cin >> a >> b; a--; b--; dfs(a, b); cout << M.size() << '\n'; M.clear(); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int Max = 1e5 + 5; const long long IFN = 1e18 + 5; inline void norm(long long &a) { a %= mod; (a < 0) && (a += mod); } inline long long modAdd(long long a, long long b) { a %= mod, b %= mod; norm(a), norm(b); return (a + b) % mod; } inline long long modSub(long long a, long long b) { a %= mod, b %= mod; norm(a), norm(b); return (a - b) % mod; } inline long long modMul(long long a, long long b) { a %= mod, b %= mod; norm(a), norm(b); return (a * b) % mod; } void F_I_O() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } long long gcd(long long a, long long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } bool ok = false; vector<bool> check(104, false); vector<pair<int, int> > v[102 + 1]; bool dfs(int u, int r, int u1) { check[u] = true; if (u == u1) { return true; } for (int i = 0; i < v[u].size(); i++) { int c = v[u][i].first; int d = v[u][i].second; if (d == r && !check[c]) { if (dfs(c, r, u1)) { return true; } } } return false; } int main() { int t = 1; while (t--) { int n, m; cin >> n >> m; int a, b, c; for (int j = 0; j < m; j++) { cin >> a >> b >> c; a--; b--; c--; v[a].push_back(make_pair(b, c)); v[b].push_back(make_pair(a, c)); } int q; cin >> q; int u, u1; for (int i = 0; i < q; i++) { cin >> u >> u1; u--; u1--; ok = false; int ans = 0; for (int j = 0; j < 100; j++) { for (int k = 0; k < 101; k++) { check[k] = false; } if (dfs(u, j, u1)) { ans++; } } cout << ans << '\n'; } } }
8
CPP
#include <bits/stdc++.h> using namespace std; const int inf = numeric_limits<int>::max(); const long long linf = numeric_limits<long long>::max(); const int max_n = 101; int n, m, q; bool g[max_n][max_n][max_n], visited[max_n]; bool dfs(int st, int end, int col, int par = -1) { if (st == end) return true; if (visited[st]) return false; visited[st] = true; bool ret = false; for (int i = 0; i < (max_n); ++i) { if (g[st][i][col] && i != par) ret |= dfs(i, end, col, st); } return ret; } int main() { int u, v, c; cin >> n; cin >> m; for (int i = 0; i < (m); ++i) { cin >> u, cin >> v, cin >> c; g[u][v][c] = true; g[v][u][c] = true; } cin >> q; for (int i = 0; i < (q); ++i) { cin >> u, cin >> v; c = 0; for (int j = (1); j < (m + 1); ++j) { fill(visited, visited + max_n, false); c += dfs(u, v, j); } cout << c << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; bool isPrime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } long long Ceil(long long a, long long b) { if (a % b == 0) return a / b; else return a / b + 1; } long long factorial(long long n) { if (n == 0) return 1; return (n * factorial(n - 1)) % 1000000007; } long long isPalindrome(string &str) { long long diff = 0; long long j = str.length() - 1; for (long long i = 0; i < j; i++, j--) { if (str[i] != str[j]) { diff++; } } return diff; } vector<pair<long long, long long>> v[102]; vector<bool> vis(102); bool dfs(long long src, long long target, long long col) { vis[src] = true; if (src == target) return true; for (auto x : v[src]) { if (vis[x.first] == false && x.second == col) { if (dfs(x.first, target, col)) return true; } } return false; } void solve() { long long n, m; cin >> n >> m; for (long long i = 0; i < m; i++) { long long a, b, c; cin >> a >> b >> c; v[a].push_back({b, c}); v[b].push_back({a, c}); } long long q; cin >> q; for (long long i = 0; i < q; i++) { long long u, v; cin >> u >> v; long long ans = 0; for (long long i = 1; i <= 100; i++) { for (long long i = 0; i < 102; i++) vis[i] = false; if (dfs(u, v, i)) ans++; } cout << ans << endl; } } int32_t main() { std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); }
8
CPP
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > g[110]; int seen[110][110]; int c[110]; int ans; void dfs(int s, int end, int color) { if (s == end && c[color] == 0) { c[color] = 1; ans++; return; } for (int i = 0; i < (int)g[s].size(); i++) { if ((color == 0 || color == g[s][i].second) && seen[s][g[s][i].first] == 0 && seen[g[s][i].first][s] == 0) { seen[s][g[s][i].first] = 1; seen[g[s][i].first][s] = 1; dfs(g[s][i].first, end, g[s][i].second); seen[s][g[s][i].first] = 0; seen[g[s][i].first][s] = 0; } } } int main() { int n, m; scanf("%d %d", &n, &m); int temp1, temp2, temp3; for (int i = 0; i < m; i++) { scanf("%d %d %d", &temp1, &temp2, &temp3); g[temp1].push_back(pair<int, int>(temp2, temp3)); g[temp2].push_back(pair<int, int>(temp1, temp3)); } int q; scanf("%d", &q); for (int i = 0; i < q; i++) { scanf("%d %d", &temp1, &temp2); dfs(temp1, temp2, 0); printf("%d\n", ans); ans = 0; for (int i = 0; i < 110; i++) for (int j = 0; j < 110; j++) seen[i][j] = 0; for (int i = 0; i < 110; i++) c[i] = 0; } return 0; }
8
CPP
import math import os from io import BytesIO, IOBase import sys # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") from collections import defaultdict n, m = map(int, input().split()) graphs = defaultdict(lambda : defaultdict(list)) for _ in range(m): a,b,c = map(int, input().split()) graphs[c][a].append(b) graphs[c][b].append(a) seen = set() def dfs(curr, orig, color): if curr in seen: return seen.add(curr) for new in graphs[color][curr]: if new != orig: dfs(new, curr, color) q = int(input()) for _ in range(q): a, b = map(int, input().split()) found = 0 for c in graphs.keys(): seen.clear() dfs(a,-1,c) if b in seen: found += 1 print(found)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<int> arr[100][100]; bool colored_dfs(int u, int v, int c, vector<vector<int> > &adj, vector<bool> &visited) { bool success = false; if (u == v) return true; for (int n : adj[u]) { for (int color : arr[u][n]) { if ((color == c) && (!visited[n])) { visited[n] = true; success = colored_dfs(n, v, c, adj, visited); if (success) return true; } } } return success; } int dfs(int u, int v, vector<vector<int> > &adj) { int ans = 0; vector<bool> visited; vector<bool> color_used(101, false); for (int n : adj[u]) { for (int c : arr[u][n]) { if (color_used[c]) continue; visited.resize(adj.size()); visited[u] = true; visited[n] = true; if (colored_dfs(n, v, c, adj, visited)) { ans++; color_used[c] = true; } visited.clear(); } } return ans; } int main() { int n, m; cin >> n >> m; vector<vector<int> > adj(n, vector<int>()); int a, b, c; for (int i = 0; i < m; i++) { cin >> a >> b >> c; a--; b--; arr[a][b].push_back(c); arr[b][a].push_back(c); int flag = 0; for (int i : adj[a]) { if (i == b) { flag = 1; break; } } if (flag) continue; adj[a].push_back(b); adj[b].push_back(a); } int q; cin >> q; vector<int> ans(q, 0); for (int i = 0; i < q; i++) { cin >> a >> b; a--; b--; ans[i] = dfs(a, b, adj); } for (int i : ans) cout << i << endl; }
8
CPP
from collections import defaultdict def main(): n, m = map(int, input().split()) edges = defaultdict(lambda: defaultdict(list)) for _ in range(m): a, b, c = map(int, input().split()) d = edges[c] d[a].append(b) d[b].append(a) def dfs(t): chain.add(t) dd = color.get(t, ()) for y in dd: if y not in chain: dfs(y) res = [] chain = set() for _ in range(int(input())): a, b = map(int, input().split()) x = 0 for color in edges.values(): chain.clear() dfs(a) if b in chain: x += 1 res.append(str(x)) print('\n'.join(res)) if __name__ == '__main__': main()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int par[105][105]; int sz[105][105]; int getpar(int x, int c) { if (par[c][x] == x) { return x; } return par[c][x] = getpar(par[c][x], c); } void dsu(int a, int b, int c) { a = getpar(a, c); b = getpar(b, c); if (a == b) { return; } if (sz[c][a] < sz[c][b]) { swap(a, b); } par[c][b] = a; sz[c][a] += sz[c][b]; } int main() { int n, m; cin >> n >> m; for (int i = 0; i < 105; i++) { for (int j = 0; j < 105; j++) { par[i][j] = j; sz[i][j] = 1; } } for (int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; dsu(a, b, c); } int k; cin >> k; for (int i = 0; i < k; i++) { int a, b; cin >> a >> b; int ans = 0; int x, y; for (int j = 0; j < 105; j++) { x = getpar(a, j); y = getpar(b, j); if (x == y) { ans++; } } cout << ans << endl; } }
8
CPP
from collections import defaultdict def color_path(graph, n, m, u, v, c): # 1 if there is a path form u to v # where all the edges have color c. # 0 otherwise. stack = [u] visited = [False] * (n + 1) while len(stack) > 0: x = stack.pop() visited[x] = True for y, cy in graph[x]: if not visited[y] and cy == c: stack.append(y) if x == v: return 1 return 0 n, m = map(int, input().split()) graph = defaultdict(list) colors = set() for _ in range(m): a, b, c = map(int, input().split()) graph[a].append((b, c)) graph[b].append((a, c)) colors.add(c) for _ in range(int(input())): u, v = map(int, input().split()) total = 0 for c in colors: total += color_path(graph, n, m, u, v, c) print(total)
8
PYTHON3
# aadiupadhyay import os.path from math import gcd, floor, ceil from collections import * import sys mod = 1000000007 INF = float('inf') def st(): return list(sys.stdin.readline().strip()) def li(): return list(map(int, sys.stdin.readline().split())) def mp(): return map(int, sys.stdin.readline().split()) def inp(): return int(sys.stdin.readline()) def pr(n): return sys.stdout.write(str(n)+"\n") def prl(n): return sys.stdout.write(str(n)+" ") if os.path.exists('input.txt'): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') n, m = mp() d = defaultdict(list) for _ in range(m): a, b, c = mp() d[(a, c)].append(b) d[(b, c)].append(a) Q = inp() v = [0 for i in range(n+1)] for i in range(Q): x, y = mp() ans = 0 for j in range(1, m+1): q = deque() q.append((x, j)) copy_visited = list(v) copy_visited[x] = 1 while q: a, b = q.popleft() if a == y: ans += 1 break for k in d[(a, b)]: if not copy_visited[k]: copy_visited[k] = 1 q.append((k, b)) print(ans)
8
PYTHON3
from collections import Counter as cntr from math import inf def cin(): return map(int, input().split(' ')) def dfs(graph, src, dest, color): global count, visited if src == dest: count += 1 return 0 for v in range(n): if graph[src][v] and visited[v]==False and color in graph[src][v]: l = graph[src][v] for j in l: if color == j: visited[v] = True dfs(graph, v, dest, color) n, m = cin() kita = [[0 for i in range(n)] for i in range(n)] clrs = [[]for i in range(n)] for i in range(m): a, b, c = cin() a -= 1 b -= 1 if kita[a][b] == 0: kita[a][b] = [c] kita[b][a] = [c] else: kita[a][b].append(c) kita[b][a].append(c) clrs[a].append(c) clrs[b].append(c) q = int(input()) for i in range(q): count = 0 u,v = cin() u -= 1 v -= 1 ll = set(clrs[u]) #print(ll) for jj in ll: visited = [False for i in range(n)] visited[u] = True dfs(kita, u, v, jj) print(count)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; long long fac[2000005] = {0}; void factorial() { fac[0] = 1; fac[1] = 1; for (long long i = 2; i < 200005; i++) fac[i] = ((i % 1000000007) * (fac[i - 1] % 1000000007)) % 1000000007; } long long power(long long n, long long m) { long long p = 1; if (m == 0) return 1; p = (power(n, m / 2) % 1000000007); p = (p % 1000000007 * p % 1000000007) % 1000000007; return (m & 1 ? ((p % 1000000007 * n % 1000000007) % 1000000007) : (p % 1000000007)); } long long ncr(long long n, long long r) { return ((fac[n] * power(fac[r], 1000000007 - 2)) % 1000000007 * power(fac[n - r], 1000000007 - 2)) % 1000000007; } vector<pair<long long, long long> > graph[102]; long long n, m; long long cnt = 0; void dfs(long long x, long long y, long long col) { long long used[102] = {0}; queue<long long> q; q.push(x); used[x] = 1; while (!q.empty()) { long long z = q.front(); q.pop(); for (long long i = 0; i < graph[z].size(); i++) { pair<long long, long long> p = graph[z][i]; if (!used[p.first]) { if (p.first == y && p.second == col) { ++cnt; return; } if (p.second == col) { q.push(p.first); used[p.first] = 1; } } } } } void ram5564() { cin >> n >> m; while (m--) { long long a, b, c; cin >> a >> b >> c; graph[a].push_back(make_pair(b, c)); graph[b].push_back(make_pair(a, c)); } long long q; cin >> q; while (q--) { long long x, y; cin >> x >> y; cnt = 0; for (long long i = 1; i < 101; i++) { dfs(x, y, i); } cout << cnt << "\n"; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; while (t--) ram5564(); return 0; }
8
CPP
from collections import defaultdict def dfs(node,b,graph,colour,vis): if node==b: return True vis[node] = True for i in graph[node]: if not vis[i[0]]: if i[1]==colour: if dfs(i[0],b,graph,i[1],vis): return True return False n,m = map(int,input().split()) graph = defaultdict(list) for i in range(m): a,b,c = map(int,input().split()) graph[a].append([b,c]) graph[b].append([a,c]) q = int(input()) visited = [False]*(n+1) for i in range(q): a,b = map(int,input().split()) ans = 0 for i in range(101): vis = visited[:] if dfs(a,b,graph,i,vis): ans+=1 print(ans)
8
PYTHON3
from sys import stdin input=stdin.readline R=lambda:map(int,input().split()) I=lambda:int(input()) def dfs(x,y,z): if x==y:return True g[x]=1 for p in cl[x]: if p[1]==z and g[p[0]]==0 and dfs(p[0],y,z):return True return False n,m=R() cl=[[] for i in range(n)] for i in range(m): a,b,c=R() cl[a-1].append([b-1,c-1]) cl[b-1].append([a-1,c-1]) q=I() s=[0]*q for i in range(q): u,v=R() for j in range(100): g=[0]*(n) if dfs(u-1,v-1,j):s[i]+=1 for i in s:print(i)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<pair<int, int>> adj[101]; bool vis[101] = {false}; bool check = false; bool dfs(int node, int vi, int color) { vis[node] = true; if (node == vi) { return true; } for (auto childnode : adj[node]) { if (!vis[childnode.first] && childnode.second == color) { if (dfs(childnode.first, vi, color)) { return true; } } } return false; } int main() { long long nodes, edges; cin >> nodes >> edges; long long ans = 0; for (int i = 0; i < edges; i++) { long long f, s, t; cin >> f >> s >> t; adj[f].push_back({s, t}); adj[s].push_back({f, t}); } int q; cin >> q; for (int i = 0; i < q; i++) { int ui, vi; cin >> ui >> vi; int ans = 0; for (int j = 1; j <= edges; j++) { memset(vis, false, sizeof(vis)); ans += dfs(ui, vi, j); } cout << ans << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, m; const int maxn = 120; vector<pair<int, int> > e[maxn]; int ans, vis[maxn], flag, book[maxn]; void dfs(int second, int t, int cur) { if (flag) return; if (second == t) { ans++; flag = 1; return; } for (auto i : e[second]) { if (vis[i.first] || i.second != cur) continue; vis[i.first] = 1; dfs(i.first, t, cur); vis[i.first] = 0; } } int main() { ios::sync_with_stdio(0), cin.tie(0); int x, y, z; cin >> n >> m; for (int i = 0; i < m; i++) { cin >> x >> y >> z; e[x].push_back({y, z}); e[y].push_back({x, z}); } int q; cin >> q; while (q--) { ans = 0; cin >> x >> y; memset(book, 0, sizeof(book)); for (auto i : e[x]) { if (book[i.second]) continue; book[i.second] = 1; flag = 0; memset(vis, 0, sizeof(vis)); vis[x] = 1; dfs(x, y, i.second); } cout << ans << endl; } return 0; }
8
CPP
# 505B from collections import defaultdict __author__ = 'artyom' read = lambda: map(int, input().split()) all_edges = defaultdict(list) n, m = read() for _ in range(m): a, b, c = read() all_edges[c].append((a, b)) graphs = [] def root(components, x, l=0): return (x, l) if components[x] == x else root(components, components[x], l + 1) def connected_components(edges): components = [i for i in range(n + 1)] for u, v in edges: ru, lu = root(components, u) rv, lv = root(components, v) if ru != rv: if lu < lv: components[ru] = rv else: components[rv] = ru return components for c, edges in all_edges.items(): graphs.append(connected_components(edges)) def count_in_same_component(u, v): count = 0 for graph in graphs: if root(graph, u)[0] == root(graph, v)[0]: count += 1 return count ans = '' for __ in range(int(input())): u, v = read() ans += str(count_in_same_component(u, v)) + '\n' print(ans)
8
PYTHON3
class Union: def __init__(self, size): self.ancestor = [i for i in range(size+1)] def find(self, node): if self.ancestor[node] == node: return node self.ancestor[node] = self.find(self.ancestor[node]) return self.ancestor[node] def merge(self, a, b): a, b = self.find(a), self.find(b) self.ancestor[a] = b n, m = map(int, input().split()) unions = [Union(n) for _ in range(m+1)] graph = [[] for _ in range(n+1)] for _ in range(m): a, b, c = map(int, input().split()) graph[a].append((b, c)) graph[b].append((a, c)) unions[c].merge(a, b) for _ in range(int(input())): a, b = map(int, input().split()) ans = 0 for i in range(1, m+1): ans += unions[i].find(a) == unions[i].find(b) print(ans)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; struct Node { int num, pre; Node(int _num = 0, int _pre = 0) { num = _num; pre = _pre; } }; int flag = 0; int cnt = 0; vector<bool> vi; void dfs(vector<vector<vector<int> > > &arc, int n, int u, int v, int c, int pre) { vi[u] = true; int ii = 0, jj = 0; if (u == v) { cnt++; flag = 1; return; } for (jj = 1; jj <= n; jj++) { if (flag == 1) break; if (vi[jj] == false && find(arc[u][jj].begin(), arc[u][jj].end(), c) != arc[u][jj].end()) { dfs(arc, n, jj, v, c, u); } } } int main() { int n = 0, m = 0; while (cin >> n >> m) { vector<int> v_tmp; vector<vector<vector<int> > > arc(n + 1, vector<vector<int> >(n + 1, v_tmp)); int i = 0, j = 0; for (i = 1; i <= m; i++) { int a = 0, b = 0, c = 0; cin >> a >> b >> c; arc[a][b].push_back(c); arc[b][a].push_back(c); } int q = 0; cin >> q; int ii = 0, jj = 0, kk = 0; for (i = 0; i < q; i++) { int u = 0, v = 0; cin >> u >> v; if (u > v) { int tmp = u; u = v; v = tmp; } cnt = 0; for (ii = 1; ii <= m; ii++) { vector<bool> visited(n + 1, false); Node uu(u, -1); queue<Node> que; que.push(uu); visited[u] = true; flag = 0; while (!que.empty() && flag == 0) { Node f = que.front(); que.pop(); for (jj = 1; jj <= n; jj++) { if (visited[jj] == false && find(arc[f.num][jj].begin(), arc[f.num][jj].end(), ii) != arc[f.num][jj].end()) { if (jj == v) { cnt++; flag = 1; break; } { Node vv(jj, f.num); que.push(vv); visited[jj] = true; } } } } } cout << cnt << endl; } } }
8
CPP
#include <bits/stdc++.h> using namespace std; vector<bool> cUsed(111, false); std::vector<bool> visited(111, false); std::vector<std::vector<std::vector<int> > > g( 111, std::vector<std::vector<int> >(111)); void dfs(int c, int u) { if (visited[u]) return; visited[u] = 1; for (int i = 0; i < g[c][u].size(); ++i) { int v = g[c][u][i]; dfs(c, v); } } int main() { int n, m, q; cin >> n >> m; for (int i = 0; i < m; ++i) { int a, b, c; cin >> a >> b >> c; cUsed[c] = 1; g[c][a].push_back(b); g[c][b].push_back(a); } cin >> q; for (int i = 0; i < q; ++i) { int u, v, ans = 0; cin >> u >> v; for (int c = 1; c <= 100; ++c) { if (!cUsed[c]) continue; fill(visited.begin(), visited.end(), 0); dfs(c, u); if (visited[v]) { ans++; } } cout << ans << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; long long gcd(long long a, long long b) { if (a == 0) return b; else return gcd(b % a, a); } long long power(long long base, long long exp, long long mod = 1000000007) { long long ans = 1; while (exp) { if (exp & 1) ans = (ans * base) % mod; exp >>= 1, base = (base * base) % mod; } return ans; } vector<pair<int, int> > v[1000]; bool visited[1000]; int check = 0; int h[1000]; void dfs(int x, int y, int clr) { visited[x] = 1; for (int i = 0; i < (int)v[x].size(); i += 1) { if (v[x][i].second == clr && visited[v[x][i].first] == 0) { if (v[x][i].first == y) check = 1; dfs(v[x][i].first, y, clr); } } } int main() { int n, m, x, y, q, clr, ans = 0; cin >> n >> m; for (int i = 0; i < m; i += 1) { cin >> x >> y >> clr; v[x].push_back(make_pair(y, clr)); v[y].push_back(make_pair(x, clr)); h[clr]++; } cin >> q; while (q--) { cin >> x >> y; ans = 0; for (clr = 1; clr <= m; clr++) { if (h[clr]) { check = 0; memset(visited, 0, sizeof(visited)); dfs(x, y, clr); if (check) ans++; } } cout << ans << "\n"; } }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 103; vector<pair<int, int>> v[maxn]; int vis[maxn]; bool dfs(int start, int des, int x) { if (vis[start]) return 0; if (start == des) return 1; vis[start] = 1; for (auto e : v[start]) { if (e.second == x) { if (dfs(e.first, des, x)) return 1; } } return 0; } int main() { ios::sync_with_stdio(0), cin.tie(0); int n, m; int x, y, z; cin >> n >> m; for (int i = 0; i < m; i++) { cin >> x >> y >> z; v[x].push_back({y, z}); v[y].push_back({x, z}); } int q; cin >> q; while (q--) { cin >> x >> y; int ans = 0; for (int i = 1; i <= m; i++) { memset(vis, 0, sizeof(vis)); if (dfs(x, y, i)) ans++; } cout << ans << endl; } return 0; }
8
CPP
def build_graph(): line1 = input().strip().split() n = int(line1[0]) m = int(line1[1]) graph = {} for _ in range(m): line = input().strip().split() u = int(line[0]) v = int(line[1]) c = int(line[2]) if c not in graph: graph[c] = {j: [] for j in range(1, n+1)} graph[c][u].append(v) graph[c][v].append(u) return graph parent_history = {} def no_of_paths(u, v, graph): x = 0 for c in graph: if c in parent_history and v in parent_history[c]: parent = parent_history[c] else: parent = {} parent = dfs_visit(v, graph[c], parent) parent_history[c] = parent if u in parent: x += 1 return x def dfs_visit(i, adj_list, parent): for j in adj_list[i]: if j not in parent: parent[j] = i dfs_visit(j, adj_list, parent) return parent if __name__ == "__main__": graph = build_graph() for _ in range(int(input())): line = input().strip().split() print(no_of_paths(int(line[0]), int(line[1]), graph))
8
PYTHON3
import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #'%.9f'%ans ########################################################## from collections import defaultdict def dfs(s,f): if s==v: return True vis[s]=1 for i in g[s]: if vis[i[0]]==0 and i[1]==f: if(dfs(i[0],f)): return True return False #for _ in range(int(input())): #n = int(input()) n,m = map(int, input().split()) g=[[] for i in range(n+2)] for i in range(m): u,v,d = map(int, input().split()) g[u].append([v,d]) g[v].append([u,d]) q= int(input()) for _ in range(q): u, v= map(int, input().split()) cnt=0 for i in range(1,101): vis=[0]*101 if (dfs(u,i)): cnt+=1 print(cnt) #g=[[] for i in range(n+1)] #arr = list(list(map(int, input().split())))
8
PYTHON3
from collections import defaultdict def dfs(g, u, visited=None): if visited is None: visited = set() visited.add(u) for node in g[u]: if node not in visited: dfs(g, node, visited) return visited def main(): n, m = map(int, input().split()) graphs = {} for _ in range(m): a, b, c = map(int, input().split()) if c not in graphs: graphs[c] = defaultdict(list) graphs[c][a].append(b) graphs[c][b].append(a) num_queries = int(input()) for _ in range(num_queries): u, v = map(int, input().split()) ans = 0 for g in graphs.values(): if v in dfs(g, u): ans += 1 print(ans) if __name__ == "__main__": main()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 105; vector<pair<int, int>> v[maxn]; int vis[maxn]; bool dfs(int start, int des, int x) { if (vis[start]) return 0; if (start == des) return 1; vis[start] = 1; for (auto e : v[start]) { if (e.second == x) { if (dfs(e.first, des, x)) return 1; } } return 0; } int main() { ios::sync_with_stdio(0), cin.tie(0); int n, m; int x, y, z; cin >> n >> m; for (int i = 0; i < m; i++) { cin >> x >> y >> z; v[x].push_back({y, z}); v[y].push_back({x, z}); } int q; cin >> q; while (q--) { cin >> x >> y; int ans = 0; for (int i = 1; i <= m; i++) { memset(vis, 0, sizeof(vis)); if (dfs(x, y, i)) ans++; } cout << ans << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 1004; vector<pair<long long, long long>> gr[N]; long long vis[N] = {}; bool dfs(long long curr, long long &col, long long &dest) { vis[curr] = 1; if (curr == dest) return true; bool ans = 0; for (auto x : gr[curr]) { if (!vis[x.first] and x.second == col) { ans = ans or dfs(x.first, col, dest); } } return ans; } void solve(long long tc) { long long n, m; cin >> n >> m; long long x, y, c; for (long long i = 0; i < m; i++) { cin >> x >> y >> c; gr[x].push_back({y, c}); gr[y].push_back({x, c}); } long long q; cin >> q; for (long long i = 0; i < q; i++) { cin >> x >> y; long long ans = 0; for (long long j = 1; j <= m; j++) { memset(vis, 0, sizeof(vis)); vis[x] = 1; ans += dfs(x, j, y); } cout << ans << "\n"; } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(1); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; struct edge { int v, color; edge(int iv, int ic) { v = iv; color = ic; } }; vector<edge> G[maxn]; bool vis[maxn]; set<int> sc; int n, m, q; int ans, s, t; bool dfs(int k, int color) { vis[k] = true; if (k == t) { return true; } for (int i = 0; i < G[k].size(); i++) { int co = G[k][i].color, v = G[k][i].v; if (co == color && !vis[v]) { if (dfs(v, co)) return true; } } return false; } int main() { cin >> n >> m; int a, b, c; memset(G, 0, sizeof(G)); for (int i = 0; i < m; i++) { scanf("%d%d%d", &a, &b, &c); G[a].push_back(edge(b, c)); G[b].push_back(edge(a, c)); } cin >> q; while (q--) { sc.clear(); scanf("%d%d", &s, &t); ans = 0; for (int i = 0; i < G[s].size(); i++) { int co = G[s][i].color; int v = G[s][i].v; memset(vis, false, sizeof(vis)); vis[s] = true; if (dfs(v, co)) sc.insert(co); } printf("%d\n", sc.size()); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void prn(bool space, Arg1&& arg1) { cout << arg1; if (space) cout << " "; else cout << "\n"; } template <typename Arg1, typename... Args> void prn(bool space, Arg1&& arg1, Args&&... args) { prn(space, arg1); prn(space, args...); } const long long mod = 1000000007; const long long MOD = 998244353; const long long inf = 1e18; const long long MAX = 2e5 + 1; inline long long add(long long a, long long b) { return ((a % mod) + (b % mod)) % mod; } inline long long sub(long long a, long long b) { return ((a % mod) - (b % mod) + mod) % mod; } inline long long mul(long long a, long long b) { return ((a % mod) * (b % mod)) % mod; } vector<long long> fac(1e6); long long pwr(long long x, long long n) { x = x % mod; if (!n) return 1; if (n & 1) return mul(x, pwr(mul(x, x), (n - 1) / 2)); else return pwr(mul(x, x), n / 2); } long long modinv(long long n) { return pwr(n, mod - 2); } long long inv(long long i) { if (i == 1) return 1; return (mod - (mod / i) * inv(mod % i) % mod) % mod; } long long ncr(long long n, long long r) { long long ans = 1; if (r != 0 && r != n) { ans = fac[n] * modinv(fac[r]) % mod * modinv(fac[n - r]) % mod; } return ans; } void pre() { fac[0] = 1; for (int i = 1; i < (int)fac.size(); i++) { fac[i] = (fac[i - 1] * i) % mod; } } bool isPowerOfTwo(int x) { return x && (!(x & (x - 1))); } vector<pair<int, int> > g[101]; int check; vector<bool> vis; int starta, enda; void dfs(int a, int color) { vis[a] = true; if (a == enda) { check = 1; return; } for (auto x : g[a]) { if (!vis[x.first] && x.second == color) { dfs(x.first, color); } } } void solve() { int n, m; cin >> n >> m; int maxa = -1; for (int i = 0; i < m; i++) { int x, y, c; cin >> x >> y >> c; maxa = max(maxa, c); g[x].push_back({y, c}); g[y].push_back({x, c}); } int q; cin >> q; while (q--) { cin >> starta >> enda; int ans = 0; for (int i = 1; i <= maxa; i++) { vis.assign(n + 1, false); check = 0; dfs(starta, i); ans += check; } cout << ans << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) { solve(); } return 0; }
8
CPP
# !/usr/bin/env python3 # coding: UTF-8 # Modified: <19/Feb/2019 06:31:43 PM> # ✪ H4WK3yE乡 # Mohd. Farhan Tahir # Indian Institute Of Information Technology (IIIT),Gwalior # Question Link # # # ///==========Libraries, Constants and Functions=============/// import sys inf = float("inf") mod = 1000000007 def get_array(): return list(map(int, sys.stdin.readline().split())) def get_ints(): return map(int, sys.stdin.readline().split()) def input(): return sys.stdin.readline() # ///==========MAIN=============/// N = 105 graph = [[] for _ in range(N)] component = [0] * N visited = [False] * N def explore(node): global cnt visited[node] = True component[node] = cnt for neighbour, color in graph[node]: if visited[neighbour] == False: explore(neighbour) def dfs(n): global cnt cnt = 0 for i in range(1, n + 1): if visited[i] == False: cnt += 1 explore(i) def explore_o(node, c): global ans, s visited[node] = True for neighbour, color in graph[node]: if visited[neighbour] == False and color == c: if neighbour == v: s.add(color) explore_o(neighbour, c) def dfs_o(u): global visited, ans, flag, s ans = 0 s = set() for neighbour, color in graph[u]: visited = [False] * N visited[u] = True flag = 0 if neighbour == v: s.add(color) continue explore_o(neighbour, color) return len(s) def main(): global n, m n, m = get_ints() for _ in range(m): u, x, c = get_ints() graph[u].append((x, c)) graph[x].append((u, c)) dfs(n) global v for tc in range(int(input())): u, v = get_ints() if component[u] != component[v]: print(0) continue print(dfs_o(u)) if __name__ == "__main__": main()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int ans[110][110] = {0}; vector<int> adj[110][110]; int vis[110]; void dfs(int parent, int u, int color) { vis[u] = 1; ans[parent][u]++; for (int v = 0; v < (int)adj[u][color].size(); v++) { if (!vis[adj[u][color][v]]) { dfs(parent, adj[u][color][v], color); } } } int main() { int n, m; cin >> n >> m; set<int> s[110]; for (int i = 0; i < m; i++) { int x, y, c; cin >> x >> y >> c; adj[x][c].push_back(y); adj[y][c].push_back(x); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { memset(vis, 0, sizeof(vis)); if ((int)adj[i][j].size() > 0) { dfs(i, i, j); } } } int q; cin >> q; for (int i = 0; i < q; i++) { int x, y; cin >> x >> y; cout << ans[x][y] << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; const int mod = 1e9 + 7; bool cmp(pair<long long, long long> a, pair<long long, long long> b) { if (a.first == b.first) return a.second < b.second; else return a.first < b.first; } long long exp(long long a, long long b) { long long ans = 1; while (b != 0) { if (b % 2) ans = ans * a; a = a * a; b /= 2; } return ans; } bool has[105]; long long comp[105]; bool vis[105]; vector<pair<long long, long long> > v[105]; void dfs(long long st, long long cmp) { vis[st] = 1; comp[st] = cmp; for (long long i = (long long)0; i < (long long)v[st].size(); i++) { long long to = v[st][i].first; if (!vis[to]) { dfs(to, cmp); } } } void dfs1(long long st, long long co) { vis[st] = 1; for (long long i = (long long)0; i < (long long)v[st].size(); i++) { long long to = v[st][i].first; long long col = v[st][i].second; if (!vis[to] && co == col) { dfs1(to, co); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m; cin >> n >> m; for (long long i = (long long)0; i < (long long)m; i++) { long long a, b, c; cin >> a >> b >> c; v[a].push_back({b, c}); v[b].push_back({a, c}); has[c] = 1; } long long q; cin >> q; for (long long i = (long long)0; i < (long long)q; i++) { long long a, b; cin >> a >> b; long long cnt = 0; for (long long i = (long long)1; i < (long long)105; i++) { if (has[i]) { memset((vis), 0, sizeof((vis))); dfs1(a, i); if (vis[b]) cnt++; } } cout << cnt << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, m; vector<pair<int, int>> v[101]; bool connected[101][101][101]; bool visited[101]; void dfs(int start, int node, int color) { int i; visited[node] = true; if (start != node) connected[start][node][color] = true; for (i = 0; i < v[node].size(); i++) { if (visited[v[node][i].first] == true) continue; if (color == v[node][i].second) dfs(start, v[node][i].first, v[node][i].second); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int i, j, a, b, c; cin >> n >> m; for (i = 0; i < m; i++) { cin >> a >> b >> c; v[a].emplace_back(b, c); v[b].emplace_back(a, c); } for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { dfs(i, i, j); memset(visited, false, sizeof(visited)); } } int q, u, v; cin >> q; for (i = 0; i < q; i++) { cin >> u >> v; int cnt = 0; for (j = 1; j <= m; j++) { if (connected[u][v][j] == true) cnt++; } cout << cnt << endl; } return 0; }
8
CPP