text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
int t;
std::cin >> t;
for (int i = 0; i < t; i++) {
int n, m;
std::cin >> n >> m;
int arr1[n], arr2[m];
std::unordered_set<int> lookup;
for (int j = 0; j < n; j++) {
std::cin >> arr1[j];
lookup.insert(arr1[j]);
}
for (int j = 0; j < m; j++) {
std::cin >> arr2[j];
}
bool ans = false;
for (int j = 0; j < m; j++) {
if (lookup.find(arr2[j]) != lookup.end()) {
std::cout << "YES\n";
std::cout << "1 " << arr2[j] << "\n";
ans = true;
break;
}
}
if (!ans) std::cout << "NO\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n, m;
scanf("%d%d", &n, &m);
vector<int> A(10001, 0);
vector<int> B(10001, 0);
int x;
for (int i = 0; i < n; i++) {
scanf("%d", &x);
A[x]++;
}
for (int i = 0; i < m; i++) {
scanf("%d", &x);
B[x]++;
}
int flag = 1;
for (int i = 0; i < 10001; i++)
if (A[i] && B[i]) {
printf("YES\n1 %d\n", i);
flag = 0;
break;
}
if (flag) printf("NO\n");
}
}
|
#include <bits/stdc++.h>
using namespace std;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
void solve() {
long long int n, m;
cin >> n >> m;
map<long long int, long long int> um1, um2;
for (int i = 0; i < (n); ++i) {
long long int num;
cin >> num;
um1[num]++;
}
for (int i = 0; i < (m); ++i) {
long long int num;
cin >> num;
um2[num]++;
}
for (auto it : um1) {
if (um2[it.first] > 0) {
cout << "YES" << '\n';
cout << 1 << " ";
cout << it.first << '\n';
return;
}
}
cout << "NO" << '\n';
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int n, m, i, j;
cin >> n >> m;
vector<long long int> a(n);
for (i = 0; i < n; i++) cin >> a[i];
vector<long long int> b(m);
for (i = 0; i < m; i++) cin >> b[i];
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (a[i] == b[j]) {
cout << "YES"
<< "\n";
cout << 1 << " " << a[i] << "\n";
return;
}
}
}
cout << "NO"
<< "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(cin.failbit);
long long int tc = 1;
cin >> tc;
while (tc--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
cin >> t;
while (t--) {
long long n, m;
cin >> n >> m;
long long a[n];
long long b[m];
map<long long, long long> mp;
for (long long i = 0; i < n; i++) {
cin >> a[i];
if (mp[a[i]] == 0) {
mp[a[i]]++;
}
}
long long ans = -1;
long long k = 0;
long long sum = 0;
for (long long i = 0; i < m; i++) {
cin >> b[i];
if (mp[b[i]] == 1) {
if (k == 0) {
ans = 0;
sum = b[i];
}
k++;
}
}
if (ans == 0) {
cout << "YES" << endl;
cout << 1 << " " << sum << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
bool sortbysec(const pair<int, int>& a, const pair<int, int>& b) {
return (a.second < b.second);
}
long long int modpower(long long int a, long long int b, long long int c) {
long long int res = 1;
while (b) {
if (b & 1LL) res = (res * a) % c;
a = (a * a) % c;
b >>= 1;
}
return res;
}
long long int gcd(long long int a, long long int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << name << " : " << arg1 << '\n';
}
using namespace std;
const int INF = 987654321;
const int MOD = 1000000007;
const int N = 1e5 + 5;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
int a[N], b[N];
for (int i = 1; i <= 1000; i++) {
a[i] = 0;
}
for (int i = 0; i < n; i++) {
int x;
cin >> x;
a[x]++;
}
int j = -1;
for (int i = 0; i < m; i++) {
cin >> b[i];
if (a[b[i]]) {
j = b[i];
}
}
if (j > 0)
cout << "YES" << '\n' << "1 " << j << '\n';
else
cout << "NO" << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int a, b, c, i, j, k, t;
cin >> t;
for (i = 0; i < t; i++) {
cin >> a >> b;
long long int h[a];
long long int h1[b];
for (j = 0; j < a; j++) {
cin >> h[j];
}
for (j = 0; j < b; j++) {
cin >> h1[j];
}
int count = 0, ans = 0;
for (j = 0; j < a; j++) {
for (k = 0; k < b; k++) {
if (h[j] == h1[k]) {
ans = h[j];
count = 1;
break;
}
}
}
if (count == 1) {
cout << "YES"
<< "\n";
cout << "1"
<< " " << ans << "\n";
} else {
cout << "NO"
<< "\n";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int t, n, m, a;
bool f1[1005], f2[1005];
int main() {
cin >> t;
while (t--) {
cin >> n >> m;
memset(f1, 0, sizeof(f1));
memset(f2, 0, sizeof(f2));
for (int i = 1; i <= n; i++) cin >> a, f1[a] = 1;
for (int i = 1; i <= m; i++) cin >> a, f2[a] = 1;
bool flag = 0;
for (int i = 0; i <= 1000; i++) {
if (f1[i] == 1 && f2[i] == 1) {
cout << "YES" << endl << 1 << " " << i << endl;
flag = 1;
break;
}
}
if (flag == 0) cout << "NO" << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m;
cin >> n >> m;
map<int, int> a, b;
int temp;
for (long long i = 0; i < n; i++) {
cin >> temp;
a[temp]++;
}
for (long long i = 0; i < m; i++) {
cin >> temp;
b[temp]++;
}
auto itr = a.end();
itr--;
for (auto it = a.begin(); it != a.end(); it++) {
if (b.find(it->first) != b.end()) {
cout << "YES" << endl << "1 " << it->first << endl;
return;
} else if (it == itr) {
cout << "NO" << endl;
return;
}
}
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
clock_t start = clock();
long long test = 1;
cin >> test;
while (test--) solve();
clock_t end = clock();
cerr << fixed << setprecision(15) << ((double)(end - start)) / CLOCKS_PER_SEC
<< endl;
return 0;
}
|
#include <bits/stdc++.h>
int main(void) {
int t;
scanf("%d\n", &t);
while (t--) {
int n, m;
scanf("%d %d\n", &n, &m);
int a[n], b[m];
for (int i = 0; i < n; i++) scanf("%d ", a + i);
for (int i = 0; i < m; i++) scanf("%d ", b + i);
int flag = 0, ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i] == b[j]) {
flag = 1;
ans = a[i];
break;
}
}
if (flag) break;
}
if (flag)
printf("YES\n1 %d\n", ans);
else
printf("NO\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int xo(long long int x, long long int y) {
return (x | y) & (~x | ~y);
}
long long int bin_Expo(long long int x, long long int n) {
if (x == 0) return 0;
if (n == 0)
return 1;
else if (n % 2 == 0)
return bin_Expo(x * x, n / 2);
else
return x * bin_Expo(x * x, (n - 1) / 2);
}
long long int mod_Expo(long long int x, long long int n, long long int M) {
if (x == 0) return 0;
if (n == 0)
return 1;
else if (n % 2 == 0)
return mod_Expo((x * x) % M, n / 2, M);
else
return (x * mod_Expo((x * x) % M, (n - 1) / 2, M)) % M;
}
bool prime_check(long long int x) {
bool prime = (x >= 2);
for (long long int i = 2; i * i <= x; i++) {
if (x % i == 0) {
prime = false;
break;
}
}
return prime;
}
long long int logg(long long int base, long long int x) {
return (long long int)(log(x) / log(base));
}
map<long long int, long long int> mm;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int TESTS = 1;
cin >> TESTS;
while (TESTS--) {
long long int k, i, j, l, h, w, q, n, m;
cin >> n >> m;
long long int a[n], b[m];
for (long long int i = 0; i < n; i++) cin >> a[i];
for (long long int i = 0; i < m; i++) cin >> b[i];
for (long long int i = 0; i < n; i++) mm[a[i]] = 1;
l = 1000000007;
for (long long int i = 0; i < m; i++) {
if (mm[b[i]] == 1) {
l = b[i];
break;
}
}
if (l != 1000000007) {
cout << "YES" << '\n';
cout << 1 << " " << l << '\n';
} else
cout << "NO" << '\n';
mm.clear();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void shammi() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main() {
shammi();
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
int A[n], B[m], freqA[1001] = {0}, freqB[1001] = {0};
for (int i = 0; i < n; i++) {
cin >> A[i];
freqA[A[i]]++;
}
for (int i = 0; i < m; i++) {
cin >> B[i];
freqB[B[i]]++;
}
int ans = 0;
if (n > m) {
for (int i = 0; i < n; i++) {
if (freqB[A[i]] > 0) {
ans = A[i];
break;
}
}
} else {
for (int i = 0; i < m; i++) {
if (freqA[B[i]] > 0) {
ans = B[i];
break;
}
}
}
if (ans != 0)
printf("YES\n%d %d\n", 1, ans);
else
printf("NO\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n, m;
cin >> n >> m;
vector<long long> num(1001, 0);
for (long long i = (0); i < (n); ++i) {
long long a;
cin >> a;
num[a] = 1;
}
for (long long i = (0); i < (m); ++i) {
long long b;
cin >> b;
if (num[b] == 1) num[b] = 2;
}
for (long long i = (1); i < (1001); ++i) {
if (num[i] == 2) {
cout << "YES\n1 " << i << "\n";
return;
}
}
cout << "NO"
<< "\n";
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long t;
t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
void init() {
freopen("cf.inp", "r", stdin);
freopen("cf.out", "w", stdout);
}
const long long N = 1e6 + 5;
long long n, a[N], b[N], m;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
cin >> n >> m;
for (long long i = 1; i <= n; i++) cin >> a[i];
for (long long i = 1; i <= m; i++) cin >> b[i];
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= m; j++) {
if (a[i] == b[j]) {
cout << "YES";
cout << endl;
cout << "1 " << a[i];
cout << endl;
goto x;
}
}
}
cout << "NO";
cout << endl;
x:;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long N = 2e5 + 5;
long long n, m, x, y, p, q, k, a, b, c, ans, res;
int main() {
long long t;
cin >> t;
while (t--) {
long long n, m;
cin >> n >> m;
map<long long, long long> ress;
long long a[n], b[m];
long long flag = 0;
for (long long i = 0; i < n; i++) {
cin >> a[i];
ress[a[i]] = 1;
}
for (long long i = 0; i < m; i++) {
cin >> b[i];
if (ress[b[i]] == 1) flag = b[i];
}
if (flag == 0)
cout << "NO" << endl;
else {
cout << "YES" << endl;
cout << 1 << " " << flag << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
long long t, i, j, a, b;
cin >> t;
for (int z = 1; z <= t; z++) {
cin >> a >> b;
int x[a], y[b];
for (i = 0; i <= a - 1; i = i + 1) cin >> x[i];
for (i = 0; i <= b - 1; i = i + 1) cin >> y[i];
for (i = 0; i <= a - 1; i = i + 1) {
for (j = 0; j <= b - 1; j = j + 1) {
if (x[i] == y[j]) {
cout << "YES"
<< "\n";
cout << "1 " << x[i] << "\n";
a = 0;
goto A;
}
}
}
A:
if (a)
cout << "NO"
<< "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int min(long long int a, long long int b) { return (a < b) ? a : b; }
long long int max(long long int a, long long int b) { return (a > b) ? a : b; }
long long int fp(long long int a, long long int b) {
if (b == 0) return 1;
long long int x = fp(a, b / 2);
x = (x * x) % 1000000007;
if (b & 1) x = (x * a) % 1000000007;
return x;
}
long long int factorial(long long int n) {
long long int fact = 1;
for (long long int i = 2; i <= n; i++) fact = fact * i;
return fact;
}
long long int ncr(long long int n, long long int r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}
long long int binomialCoeff(long long int n, long long int k) {
long long int dp[k + 1];
memset(dp, 0, sizeof(dp));
dp[0] = 1;
for (long long int i = 1; i <= n; i++) {
for (long long int j = min(i, k); j > 0; j--)
dp[j] = (dp[j] + dp[j - 1]) % 1000000007;
}
return dp[k] % 1000000007;
}
void c_p_c() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
}
signed main() {
c_p_c();
long long int t;
cin >> t;
while (t--) {
long long int n, m;
cin >> n >> m;
long long int a[n];
for (long long int i = 0; i < n; i++) cin >> a[i];
long long int b[m];
for (long long int i = 0; i < m; i++) cin >> b[i];
long long int f = 0;
for (long long int i = 0; i < n; i++) {
for (long long int j = 0; j < m; j++) {
if (a[i] == b[j]) {
f = 1;
cout << "YES"
<< "\n";
cout << 1 << " " << a[i] << "\n";
break;
}
}
if (f) break;
}
if (!f)
cout << "NO"
<< "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, m, n, a[1000], b[1000], mi, ni, f, v;
cin >> t;
while (t) {
cin >> m;
cin >> n;
for (mi = 0; mi < m; mi++) cin >> a[mi];
for (ni = 0; ni < n; ni++) cin >> b[ni];
mi = 0;
v = 0;
while (mi < m && v == 0) {
ni = 0;
while (ni < n && v == 0) {
if (a[mi] == b[ni]) {
f = a[mi];
v = 1;
}
ni++;
}
mi++;
}
if (v) {
cout << "YES" << endl << v << " " << f << endl;
} else {
cout << "NO" << endl;
}
t--;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t, n, res, m, x, flag;
cin >> t;
while (t--) {
vector<long long> v1, v2;
vector<long long>::iterator it;
cin >> n >> m;
flag = 0;
for (long long i = 0; i < n; i++) {
cin >> x;
v1.push_back(x);
}
for (long long i = 0; i < m; i++) {
cin >> x;
v2.push_back(x);
}
for (long long i = 0; i < n; i++) {
it = find(v2.begin(), v2.end(), v1[i]);
if (it != v2.end()) {
res = *it;
flag = 1;
break;
}
}
if (flag == 1) {
cout << "YES\n";
cout << "1 " << res << "\n";
} else {
cout << "NO\n";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long a[1009], b[1009], n, m, t, ans;
int main() {
cin >> t;
while (t--) {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) cin >> b[i];
ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i] == b[j]) ans = a[i];
}
}
if (!ans)
cout << "NO\n";
else
cout << "YES\n1 " << ans << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
int a[n + 1], b[m + 1];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> b[i];
}
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i] == b[j]) {
count++;
cout << "YES" << endl;
cout << 1 << " " << a[i] << endl;
break;
}
}
if (count == 1) {
break;
}
}
if (count == 0) {
cout << "NO" << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
signed main() {
long long int t;
cin >> t;
while (t--) {
long long int n, m;
cin >> n >> m;
long long int arr[1001] = {0};
for (long long int i = 0; i < n; i++) {
long long int x;
cin >> x;
arr[x]++;
}
bool ans = true;
for (long long int i = 0; i < m; i++) {
long long int x;
cin >> x;
if (arr[x] > 0 && ans) {
cout << "YES" << endl;
cout << 1 << " " << x << endl;
ans = false;
}
}
if (ans) cout << "NO" << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m, a[1000], b[1000], c[1000], k = 0, j;
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) cin >> b[i];
for (int i = 0; i < m; i++) {
j = 0;
while (k == 0 && j < n) {
if (a[j] == b[i]) {
c[k] = b[i];
k++;
break;
}
j++;
}
}
if (k == 0)
cout << "NO" << endl;
else {
cout << "YES" << endl;
cout << k << " ";
cout << c[k - 1];
}
cout << endl;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t, m, n, i, j, p, c;
long long int x[100000], y[100000];
cin >> t;
while (t--) {
p = 0;
cin >> n >> m;
for (i = 0; i < n; i++) {
cin >> x[i];
}
for (i = 0; i < m; i++) {
cin >> y[i];
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (x[i] == y[j]) {
p = 1;
c = x[i];
break;
}
}
if (p) {
break;
}
}
if (p) {
cout << "YES" << endl;
cout << p << " " << c << endl;
} else {
cout << "NO" << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
long long n, m, i, j, k;
set<long long> a, b;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
if (0)
;
long long t;
cin >> t;
while (t--) {
a.clear();
b.clear();
cin >> n >> m;
for (i = 0; i < n; i++) {
cin >> k;
a.insert(k);
}
for (i = 0; i < m; i++) {
cin >> k;
b.insert(k);
}
for (i = 1; i <= 1000; i++) {
if (a.count(i) > 0 && b.count(i) > 0) {
cout << "YES" << endl << 1 << " " << i << endl;
break;
}
}
if (i == 1001) cout << "NO" << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T1, class T2>
ostream& operator<<(ostream& s, pair<T1, T2> P) {
return s << '<' << P.first << ", " << P.second << '>';
}
template <class T>
ostream& operator<<(ostream& s, vector<T> P) {
for (long long i = 0; i < P.size(); ++i) {
if (i > 0) {
s << " ";
}
s << P[i];
}
return s;
}
template <class T>
ostream& operator<<(ostream& s, vector<vector<T> > P) {
for (long long i = 0; i < P.size(); ++i) {
s << endl << P[i];
}
return s << endl;
}
template <class T>
ostream& operator<<(ostream& s, set<T> P) {
for (__typeof__((P).begin()) it = (P).begin(); it != (P).end(); ++it) {
s << "<" << *it << "> ";
}
return s << endl;
}
template <class T1, class T2>
ostream& operator<<(ostream& s, map<T1, T2> P) {
for (__typeof__((P).begin()) it = (P).begin(); it != (P).end(); ++it) {
s << "<" << it->first << "->" << it->second << "> ";
}
return s << endl;
}
template <class T>
void show(vector<T> v) {
for (long long i = 0; i < v.size(); i++) {
cerr << v[i] << " ";
}
cerr << "\n";
}
void solve() {
long long n, m;
cin >> n >> m;
vector<long long> a(n), b(m);
vector<long long> v(10010, 0);
for (long long i = 0; i < n; i++) {
cin >> a[i];
v[a[i]]++;
}
bool ok = false;
long long x;
for (long long i = 0; i < m; i++) {
cin >> b[i];
if (v[b[i]] > 0) {
ok = true;
x = b[i];
}
}
if (ok) {
cout << "YES" << endl;
cout << 1 << " " << x << endl;
} else {
cout << "NO" << endl;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int i, j, k, n, m;
cin >> n >> m;
vector<long long int> a(n), b(m);
for (auto &i : a) cin >> i;
;
for (auto &i : b) cin >> i;
;
map<long long int, long long int> m2;
for (i = 0; i < n; i++) m2[a[i]]++;
for (i = 0; i < m; i++) {
if (m2[b[i]]) {
cout << "YES\n";
cout << 1 << " " << b[i] << '\n';
return;
}
}
cout << "NO\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int t = 1;
cin >> t;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
srand(chrono::duration_cast<chrono::nanoseconds>(
chrono::high_resolution_clock::now().time_since_epoch())
.count());
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
map<int, int> z;
for (int i = 0; i < n; ++i) {
int val;
cin >> val;
z[val] = 1;
}
int found = -1;
for (int j = 0; j < m; ++j) {
int val;
cin >> val;
if (z[val]) found = val;
}
if (found == -1) {
cout << "NO\n";
} else {
cout << "YES\n" << 1 << ' ' << found << '\n';
}
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC target("popcnt")
using namespace std;
int a[1003], b[1003];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T;
cin >> T;
while (T--) {
int N, M, temp;
cin >> N >> M;
set<int> a;
for (int i = 0; i < N; ++i) {
cin >> temp;
a.insert(temp);
}
for (int i = 0; i < M; ++i) {
cin >> b[i];
}
int ans = -1;
for (int i = 0; i < M; ++i) {
if (a.find(b[i]) != a.end()) {
ans = b[i];
break;
}
}
if (ans == -1) {
cout << "NO" << endl;
} else {
cout << "YES" << endl << "1 " << ans << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
bool a[1005];
int main() {
int t, n, m, k;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &m);
for (int i = 0; i < 1001; i++) {
a[i] = 0;
}
for (int i = 0; i < n; i++) {
scanf("%d", &k);
a[k] = 1;
}
bool f = 0;
for (int i = 0; i < m; i++) {
scanf("%d", &k);
if (f) continue;
if (a[k]) {
f = 1;
printf("YES\n1 %d\n", k);
}
}
if (!f) printf("NO\n");
}
return 0;
}
|
#include <bits/stdc++.h>
const long long infl = 1e18;
const int infi = 2e9;
const int mod = 1e9 + 7;
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
long long n, m;
cin >> n >> m;
long long ans;
long long a[100000];
long long b[100000];
long long count = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> b[i];
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (a[i] == b[j]) {
count++;
ans = a[i];
break;
}
}
if (count) {
cout << "YES"
<< "\n";
cout << 1 << " " << ans << "\n";
} else {
cout << "NO"
<< "\n";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int a[2000];
int b[2000];
int main() {
int m;
cin >> m;
while (m--) {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
for (int j = 0; j < k; j++) cin >> b[j];
sort(a, a + n);
sort(b, b + k);
int num = 0;
int flag;
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
flag = 0;
if (b[j] == a[i]) {
flag = 1;
num = a[i];
break;
}
}
if (flag == 1) break;
}
if (num != 0) {
cout << "YES" << endl;
;
cout << 1 << ' ' << num << endl;
} else
cout << "NO" << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
long long t;
cin >> t;
while (t--) {
long long n, m, a;
cin >> n >> m;
set<long long> s;
for (long long i = 0; i < n; i++) {
cin >> a;
s.insert(a);
}
long long f = -1;
for (long long i = 0; i < m; i++) {
cin >> a;
if (s.find(a) != s.end()) {
f = a;
}
}
if (f == -1)
cout << "NO"
<< "\n";
else {
cout << "YES"
<< "\n"
<< 1 << " " << f << "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int x, y;
int flag = 0;
cin >> x >> y;
int a;
vector<int> b(y);
set<int> s;
for (int i = 0; i < x; i++) {
cin >> a;
s.insert(a);
}
for (int j = 0; j < y; j++) {
cin >> b[j];
}
for (int i = 0; i < y; i++) {
if (s.find(b[i]) != s.end()) {
cout << "YES" << endl;
cout << 1 << " " << b[i] << endl;
flag = 1;
break;
}
}
if (flag == 0) {
cout << "NO" << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> graph[200001];
vector<int> visit;
long long int power(int n) {
if (n == 0) return 1;
if (n == 1) return 2;
if (n % 2)
return ((((power(n / 2)) % 1000000007) * (power(n / 2) % 1000000007) %
1000000007) *
2) %
1000000007;
return (((power(n / 2)) % 1000000007) * (power(n / 2) % 1000000007) %
1000000007);
}
void solve() {
int i, n, m;
cin >> n >> m;
int a[1001] = {0}, b[1001] = {0};
int arr;
for (i = 0; i < n; i++) {
cin >> arr;
a[arr] = 1;
}
for (i = 0; i < m; i++) {
cin >> arr;
b[arr] = 1;
}
for (i = 1; i <= 1000; i++)
if (a[i] == b[i] && a[i] == 1) {
cout << "YES" << endl << 1 << " " << i << endl;
return;
}
cout << "NO" << endl;
}
int main() {
int t;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n, m;
cin >> n >> m;
long long int a[n], b[m];
map<long long int, long long int> hm;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (hm[a[i]] == 0) hm[a[i]]++;
}
for (int i = 0; i < m; i++) {
cin >> b[i];
if (hm[b[i]] == 1) hm[b[i]]++;
}
int flag = 1;
for (auto i = hm.begin(); i != hm.end(); i++) {
if (i->second > 1) {
flag = 0;
cout << "YES" << endl;
cout << "1 " << i->first << endl;
break;
}
}
if (flag == 1) {
cout << "NO" << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int main() {
long long tst;
cin >> tst;
while (tst--) {
long long n, m;
cin >> n >> m;
map<long long, long long> was;
for (long long i = 0; i < n; i++) {
long long x;
cin >> x;
was[x] = 1;
}
bool ok = true;
long long ans;
for (long long i = 0; i < m; i++) {
long long x;
cin >> x;
if (was[x] && ok) {
ok = false;
ans = x;
}
}
if (ok)
cout << "NO" << endl;
else {
cout << "YES" << endl;
cout << 1 << ' ' << ans << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin >> t;
while (t--) {
long long n, m, e = 0;
cin >> n >> m;
long long a[n], b[m];
for (long long i = 0; i < n; i++) cin >> a[i];
for (long long i = 0; i < m; i++) cin >> b[i];
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
if (a[i] == b[j]) {
e = a[i];
break;
}
}
if (e) break;
}
if (e) {
cout << "YES\n";
cout << 1 << " " << e << "\n";
} else
cout << "NO\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m;
cin >> n >> m;
bool frq[1001] = {false};
int val;
for (int i = 0; i < n; i++) {
cin >> val;
frq[val] = true;
}
int b[m];
for (int i = 0; i < m; i++) cin >> b[i];
for (int i = 0; i < m; i++) {
if (frq[b[i]]) {
cout << "YES"
<< "\n"
<< 1 << " " << b[i] << "\n";
return;
}
}
cout << "NO"
<< "\n";
return;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using namespace std;
int main() {
int cases;
cin >> cases;
while (cases--) {
int n, m;
cin >> n >> m;
int one[n], two[m];
for (int i = 0; i < n; i++) {
cin >> one[i];
}
for (int i = 0; i < m; i++) {
cin >> two[i];
}
int arr1[1000], arr2[1000];
for (int i = 0; i < 1000; i++) {
arr1[i] = 0;
arr2[i] = 0;
}
for (int i = 0; i < n; i++) {
arr1[one[i] - 1]++;
}
for (int i = 0; i < m; i++) {
arr2[two[i] - 1]++;
}
int temp = 0;
for (int i = 0; i < 1000; i++) {
if (arr1[i] != 0 && arr2[i] != 0) {
temp = i + 1;
break;
}
}
if (temp) {
cout << "YES" << endl;
cout << "1"
<< " " << temp << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mx = 1e5 + 10;
const double PI = cos(-1.0);
map<int, int> a, b;
int n, m, t;
int main() {
cin >> t;
while (t--) {
int x;
cin >> n >> m;
a.clear();
b.clear();
for (int i = 1; i <= n; i++) {
cin >> x;
a[x] = 1;
}
for (int i = 1; i <= m; i++) {
cin >> x;
b[x] = 1;
}
int f = 0;
for (int i = 1; i <= 1000; i++) {
if (a[i] && b[i]) {
f = 1;
printf("YES\n1 %d\n", i);
break;
}
}
if (f == 0) printf("NO\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin >> t;
while (t--) {
long long n, m;
cin >> n >> m;
vector<long long> a(n);
vector<long long> b(m);
for (long long i = 0; i < n; i++) cin >> a[i];
for (long long i = 0; i < m; i++) cin >> b[i];
long long cnt = 0;
long long ans = -1;
for (long long i = 0; i < n; i++) {
if (cnt == 1)
break;
else {
for (long long j = 0; j < m; j++) {
if (a[i] == b[j]) {
cnt = 1;
ans = a[i];
break;
}
}
}
}
if (cnt == 0) {
cout << "NO\n";
} else {
cout << "YES\n"
<< "1 " << ans << "\n";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
bool comp(int a, int b) { return a < b; }
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
;
int64_t t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
bool flag = true;
int first[n];
for (int i = 0; i < n; i++) cin >> first[i];
int second[m];
for (int i = 0; i < m; i++) cin >> second[i];
std::vector<int> v1(1000, 0);
std::vector<int>::iterator it;
std::sort(first, first + n);
std::sort(second, second + m);
it =
std::set_intersection(first, first + n, second, second + m, v1.begin());
v1.resize(it - v1.begin());
if (v1.size() > 0)
cout << "YES\n" << 1 << " " << *min_element(v1.begin(), v1.end()) << '\n';
else
cout << "NO\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, i, j, c, n, m, flag;
int A[10000], B[10000];
cin >> t;
while (t--) {
cin >> n >> m;
for (i = 0; i < n; i++) cin >> A[i];
for (i = 0; i < m; i++) cin >> B[i];
flag = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (A[i] == B[j]) {
c = A[i];
flag = 1;
break;
}
}
}
if (flag == 0)
cout << "NO" << endl;
else {
cout << "YES" << endl;
cout << 1 << " " << c << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long int find_ans(long long int x, long long int k, long long int a[],
long long int n) {
long long int ans = 0;
for (long long int i = 0; i < n; i++) ans += pow(abs(a[i] - x), k);
return ans;
}
int32_t main() {
long long int tt;
cin >> tt;
while (tt--) {
long long int n, m;
cin >> n >> m;
long long int a[n], b[m];
for (long long int i = 0; i < n; i++) cin >> a[i];
for (long long int i = 0; i < m; i++) cin >> b[i];
long long int ans = -1;
for (long long int i = 0; i < n; i++) {
for (long long int j = 0; j < m; j++) {
if (a[i] == b[j]) {
ans = a[i];
break;
}
}
}
if (ans == -1) {
cout << "NO";
} else {
cout << "YES\n";
cout << "1 " << ans;
}
cout << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
int a[n], b[m], i, j;
for (i = 0; i < n; i++) cin >> a[i];
for (i = 0; i < m; i++) cin >> b[i];
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (a[i] == b[j]) {
cout << "YES" << endl
<< "1"
<< " " << a[i] << endl;
goto s;
}
}
}
cout << "NO" << endl;
s : {}
}
}
|
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long power(long long a, long long n) {
a %= 1000000007;
if (n == 1) return a;
if (n == 0) return 1;
if (n % 2)
return (a * (power((a * a) % 1000000007, n / 2) % 1000000007)) % 1000000007;
return power((a * a) % 1000000007, n / 2) % 1000000007;
}
const long long inf = (long long)1e18;
long long inverse(long long x) { return power(x, 1000000007 - 2) % 1000000007; }
void solve() {
long long n, m;
cin >> n >> m;
vector<long long> a(n + 5), b(m + 5), c1(1005), c2(1005);
for (long long i = 1; i < n + 1; ++i) cin >> a[i], c1[a[i]] = 1;
for (long long i = 1; i < m + 1; ++i) cin >> b[i], c2[b[i]] = 1;
for (long long i = 1; i < 1001; ++i)
if (c1[i] > 0 && c2[i] > 0) {
cout << "YES\n" << 1 << " " << i << "\n";
return;
}
cout << "NO\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long T = 1;
cin >> T;
for (long long t = 1; t < T + 1; ++t) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n, m;
cin >> t;
for (int z = 1; z <= t; z++) {
cin >> n;
cin >> m;
int arr[n];
int brr[m];
int flag = 0;
int pos = -1;
set<int> s;
for (int i = 0; i < n; i++) {
cin >> arr[i];
s.insert(arr[i]);
}
for (int i = 0; i < m; i++) {
cin >> brr[i];
if (s.find(brr[i]) != s.end()) {
flag = 1;
pos = i;
}
}
if (pos == -1)
cout << "NO" << endl;
else {
cout << "YES" << endl;
cout << 1 << " ";
cout << brr[pos] << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool flag;
int t, n[1010], m[1010];
const int MAXN = 1e3 + 10;
int a[MAXN][MAXN], b[MAXN][MAXN];
int main() {
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n[i] >> m[i];
for (int j = 0; j < n[i]; j++) cin >> a[i][j];
for (int j = 0; j < m[i]; j++) cin >> b[i][j];
}
for (int i = 0; i < t; i++) {
flag = 0;
for (int j = 0; j < n[i]; j++) {
for (int k = 0; k < m[i]; k++) {
if (a[i][j] == b[i][k]) {
cout << "YES" << endl;
cout << 1 << " " << a[i][j] << endl;
flag = 1;
break;
}
}
if (flag == 1) break;
}
if (flag == 0) cout << "NO" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
using namespace std;
long long int t, n, m, a[1005], b[1005];
map<long long int, long long int> cnt1, cnt2;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> t;
while (t--) {
cin >> n >> m;
cnt1.clear();
cnt2.clear();
for (long long int i = 1; i <= n; i++) {
cin >> a[i];
cnt1[a[i]]++;
}
for (long long int i = 1; i <= m; i++) {
cin >> b[i];
cnt2[b[i]]++;
}
for (long long int i = 1; i <= 1005; i++) {
if (cnt1[i] && cnt2[i]) {
cout << "YES" << '\n';
cout << 1 << ' ' << i << '\n';
goto End;
}
}
cout << "NO" << '\n';
End:
n = 0;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, m, k = 0;
cin >> n >> m;
int a[n + 2];
int b[m + 2];
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) cin >> b[i];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i] == b[j]) {
cout << "YES" << endl;
cout << "1"
<< " " << a[i] << endl;
k++;
break;
}
}
if (k > 0) break;
}
if (k == 0) cout << "NO" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m, a[1000], b[1000], c[1000], k = 0, j;
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) {
cin >> b[i];
j = 0;
while (k == 0 && j < n) {
if (a[j] == b[i]) {
c[k] = b[i];
k++;
break;
}
j++;
}
}
if (k == 0)
cout << "NO" << endl;
else {
cout << "YES" << endl;
cout << k << " ";
cout << c[k - 1];
}
cout << endl;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long int A, B, C, D, E, H, W, M, K, L, R, N, X, Y, Z, num = 0, sum = 0,
flag = 0;
string S, T;
cin >> Z;
while (Z--) {
cin >> N >> M;
vector<long long int> P(N, 0);
vector<long long int> Q(M, 0);
for (long long int i = 0; i < N; i++) cin >> P[i];
for (long long int i = 0; i < M; i++) cin >> Q[i];
sort(P.begin(), P.end());
sort(Q.begin(), Q.end());
num = -1;
for (long long int i = 0; i < N; i++)
for (long long int j = 0; j < M; j++)
if (P[i] == Q[j]) num = P[i];
if (num == -1)
cout << "NO" << endl;
else {
cout << "YES" << endl;
cout << 1 << " " << num << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int m[1005];
void solve() {
int n, m1;
cin >> n >> m1;
for (int i = 1; i <= 1003; i++) m[i] = 0;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
m[x]++;
}
int y = 0;
for (int i = 1; i <= m1; i++) {
int x;
cin >> x;
if (m[x] > 0) {
y = x;
}
}
if (y == 0)
cout << "NO" << endl;
else {
cout << "YES" << endl;
cout << 1 << ' ' << y << endl;
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin >> t;
for (int i = 1; i <= t; i++) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
int t;
cin >> t;
while (t--) {
bool h = 0;
cin >> n >> m;
int an[n], am[m];
for (int i = 0; i < n; i++) cin >> an[i];
for (int i = 0; i < m; i++) cin >> am[i];
sort(an, an + n);
sort(am, am + m);
int i = 0, j = 0, k = 0;
while (i < n && j < m) {
if (an[i] == am[j]) {
k = an[i];
h = 1;
break;
}
if (an[i] > am[j]) j++;
if (an[i] < am[j]) i++;
}
if (h)
cout << "YES" << endl << 1 << ' ' << k << endl;
else
cout << "NO" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
set<int> s;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
s.insert(x);
}
bool is_no = true;
for (int i = 0; i < m; i++) {
int x;
cin >> x;
if (s.count(x) && is_no) {
cout << "YES" << endl;
cout << 1 << " " << x << endl;
is_no = false;
}
}
if (is_no) {
cout << "NO" << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool compare(int a, int b) { return a < b; }
long long int modd(long long int n, long long int k) {
return (k + (n % k)) % k;
}
long long int gcd(long long int a, long long int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long int fastexp(long long int a, long long int b) {
long long int res = 1;
while (b > 0) {
if ((b & 1) != 0) res = (res * a);
a = (a * a);
b >>= 1;
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int tc;
cin >> tc;
while (tc--) {
long long int n, m;
cin >> n >> m;
long long int a[n], b[m];
for (long long int i = 0; i < n; i++) cin >> a[i];
for (long long int i = 0; i < m; i++) cin >> b[i];
long long int freq[2000] = {0};
for (long long int i = 0; i < n; i++) {
freq[a[i]] = 1;
}
long long int flag = -1;
for (long long int i = 0; i < m; i++) {
if (freq[b[i]] == 1) flag = b[i];
}
if (flag == -1)
cout << "NO\n";
else
cout << "YES\n"
<< "1 " << flag << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
for (int t = 0; t < T; ++t) {
int n;
int m;
cin >> n >> m;
vector<int> a;
vector<int> b;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
a.push_back(x);
}
for (int i = 0; i < m; ++i) {
int x;
cin >> x;
b.push_back(x);
}
int common_num = -1;
bool found = false;
for (int i = 0; i < n; ++i) {
if (found) {
break;
}
for (int j = 0; j < m; ++j) {
if (a[i] == b[j]) {
found = true;
common_num = a[i];
break;
}
}
}
if (!found) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
cout << 1 << " " << common_num << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, m, c = 0, x;
cin >> n >> m;
int a[n], b[m];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> b[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i] == b[j]) {
c = 1;
x = a[i];
break;
}
}
if (c == 1) {
break;
}
}
if (c == 0) {
cout << "NO\n";
} else {
cout << "YES\n";
cout << 1 << " " << x << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > v;
stack<char> sta;
map<long long, long long> mp, pm;
priority_queue<long long> pq;
set<long long> st;
set<long long>::iterator P;
string s;
long long A[2000001], B[2000001], i, j, n, m, t, k, a, b, c, maxe, mine, sum;
char z;
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
cin >> t;
while (t--) {
k = 0;
map<long long, long long> mp, pm;
cin >> n >> m;
for (i = 0; i < n; i++) cin >> A[i];
for (i = 0; i < m; i++) cin >> B[i], mp[B[i]]++;
for (i = 0; i < n; i++)
if (mp[A[i]]) break;
if (i == n)
cout << "NO" << endl;
else
cout << "YES" << endl << 1 << " " << A[i] << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t;
cin >> t;
while (t-- > 0) {
long long int n, m;
cin >> n >> m;
long long int a[n], b[m];
unordered_map<long long int, long long int> map;
for (int i = 0; i < n; i++) {
cin >> a[i];
map[a[i]]++;
}
bool boo = false;
for (int i = 0; i < m; i++) {
cin >> b[i];
}
for (int i = 0; i < m; i++) {
if (map.find(b[i]) != map.end()) {
boo = true;
cout << "YES" << endl;
cout << "1 " << b[i] << endl;
break;
}
}
if (!boo) {
cout << "NO" << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int tc;
cin >> tc;
while (tc--) {
int n, m, c = 0;
cin >> n >> m;
int a[n], b[m];
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) {
cin >> b[i];
for (int j = 0; j < n; j++) {
if (b[i] == a[j]) {
c = b[i];
break;
}
if (c) break;
}
}
if (c)
cout << "YES\n1 " << c << endl;
else
cout << "NO\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, m, temp = 0;
cin >> n >> m;
int a[n], b[m];
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) cin >> b[i];
for (int i = 0; i < n; i++) {
if (temp > 0) break;
for (int j = 0; j < m; j++) {
if (a[i] == b[j]) {
temp = a[i];
break;
}
}
}
if (temp > 0) {
cout << "YES" << endl;
cout << "1"
<< " " << temp << endl;
} else
cout << "NO" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const char nl = '\n';
void solve() {
int n, m;
cin >> n >> m;
int a[n], b[m];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> b[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i] == b[j]) {
cout << "YES" << nl << 1 << " " << a[i] << nl;
return;
}
}
}
cout << "NO" << nl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin >> t;
while (t--) {
int a, b;
int r = 0;
cin >> a >> b;
vector<int> A, B;
vector<int> v(a + b);
for (int i = 0; i < a; i++) {
cin >> r;
A.push_back(r);
}
for (int i = 0; i < b; i++) {
cin >> r;
B.push_back(r);
}
sort(A.begin(), A.end());
sort(B.begin(), B.end());
auto it =
set_intersection(A.begin(), A.end(), B.begin(), B.end(), v.begin());
bool printed = false;
if (it != B.end()) {
for (auto itr = v.begin(); itr != it; ++itr) {
cout << "YES\n"
<< "1 " << *itr << "\n";
printed = true;
break;
}
}
if (printed == false) {
cout << "NO"
<< "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int KINF = 0x3f3f3f3f;
int main() {
ios::sync_with_stdio(false);
int _;
cin >> _;
while (_--) {
int n, m;
cin >> n >> m;
int mp[1010];
memset(mp, 0, sizeof mp);
bool flag = false;
for (int i = 0; i < n; ++i) {
int num;
cin >> num;
mp[num]++;
}
for (int i = 0; i < m; ++i) {
int num;
cin >> num;
if (mp[num] > 0 && !flag) {
cout << "YES" << endl;
cout << 1 << " " << num << endl;
flag = true;
}
}
if (!flag) cout << "NO" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
set<int> s;
vector<int> a(n), b(m);
for (int i = 0; i < n; i++) {
cin >> a[i];
s.insert(a[i]);
}
for (int i = 0; i < m; i++) {
cin >> b[i];
}
vector<int> c;
for (int i = 0; i < m; i++) {
if (s.count(b[i])) {
c.push_back(b[i]);
break;
}
}
if (c.size()) {
cout << "YES"
<< "\n";
cout << 1 << " " << c[0] << "\n";
} else {
cout << "NO"
<< "\n";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m;
cin >> n >> m;
int a[n], b[m];
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) cin >> b[i];
sort(a, a + n);
sort(b, b + m);
int i = 0, j = 0;
while (i < n && j < m) {
if (a[i] == b[j]) {
cout << "YES" << endl;
cout << 1 << " " << a[i] << endl;
return;
} else if (a[i] < b[j])
i++;
else
j++;
}
cout << "NO" << endl;
return;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
vector<bool> seen(2000);
for (int i = 0; i < n; i++) {
int k;
cin >> k;
seen[k] = true;
}
int ans = -1;
for (int j = 0; j < m; j++) {
int k;
cin >> k;
if (seen[k]) {
ans = k;
}
}
if (ans == -1)
cout << "NO"
<< "\n";
else {
cout << "YES"
<< "\n";
cout << 1 << " " << ans << "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t, x, n, m, r[1001], s;
cin >> t;
while (t--) {
cin >> n >> m;
for (x = 0; x < n; x++) cin >> r[x];
for (x = 0; x < m; x++) {
cin >> s;
if (count(r, r + n, s)) break;
}
if (x < m) {
for (x++; x < m; m--) cin >> n;
cout << "YES\n1 " << s << endl;
} else
cout << "NO\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n, m, i, j;
cin >> t;
while (t--) {
cin >> n >> m;
int a[n];
int b[m];
for (i = 0; i < n; i++) {
cin >> a[i];
}
for (i = 0; i < m; i++) {
cin >> b[i];
}
bool flag = false;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (a[i] == b[j]) {
flag = true;
break;
}
}
if (flag == true) break;
}
if (flag == true)
cout << " YES\n" << 1 << " " << a[i] << "\n";
else
cout << " NO\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long int> v;
set<long long int> s1;
set<long long int> s2;
set<string> st2;
vector<long long int> v1;
vector<long long int> v2;
vector<long long int> v3;
vector<long long int> v4;
vector<long long int> v5;
vector<pair<long long int, long long int> > v6;
vector<pair<long long int, long long int> > v16;
vector<string> v7;
vector<string> v8;
vector<char> v9;
vector<double> v11;
list<long long int> l;
int main() {
long long int t;
cin >> t;
while (t-- > 0) {
long long int n, m;
cin >> n >> m;
long long int a[n], b[m];
long long int c[1001];
for (long long int i = 0; i < n; i++) {
cin >> a[i];
}
for (long long int i = 0; i < m; i++) {
cin >> b[i];
}
for (long long int i = 0; i < 1001; i++) {
c[i] = 0;
}
for (long long int i = 0; i < n; i++) {
c[a[i]] = 1;
}
long long int c1 = 0;
long long int m1;
for (long long int i = 0; i < m; i++) {
if (c[b[i]] == 1) {
c1++;
m1 = b[i];
break;
}
}
if (c1 == 0)
cout << "NO" << endl;
else {
cout << "YES" << endl;
cout << 1 << " " << m1 << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int seach(int c, int arr[], int size) {
for (int i = 0; i < size; i++) {
if (arr[i] == c) {
return 1;
}
}
return 0;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int k1;
cin >> k1;
int k2;
cin >> k2;
int arr1[k1];
for (int j = 0; j < k1; j++) {
int c;
cin >> c;
arr1[j] = c;
}
bool p = false;
for (int j = 0; j < k2; j++) {
int c;
cin >> c;
if (seach(c, arr1, k1) == 1) {
if (!p) cout << "Yes\n1 " << c << endl;
p = true;
}
}
if (!p) cout << "No\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void input(vector<T> &arr, long long int n) {
T temp;
for (long long int i = 0; i < n; i++) cin >> temp, arr.push_back(temp);
}
template <typename T>
void output(vector<T> arr) {
T temp;
for (auto x : arr) cout << x << " ";
cout << endl;
}
template <typename T>
void input_set(set<T> &arr, long long int n) {
T temp;
for (long long int i = 0; i < n; i++) cin >> temp, arr.insert(temp);
}
long long int power(long long int num, long long int base) {
if (base == 0) return 1;
if (base % 2)
return (num % 1000000007 * power(num, base - 1) % 1000000007) % 1000000007;
else {
long long int x = power(num, base / 2);
x = (x * x) % 1000000007;
return x;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int testcases;
cin >> testcases;
while (testcases--) {
long long int n, m, k, i, ans = -1;
cin >> n >> m;
map<long long int, long long int> m1;
for (i = 0; i < n; i++) {
cin >> k;
m1[k]++;
}
for (i = 0; i < m; i++) {
cin >> k;
if (m1[k] && ans == -1) ans = k;
}
if (ans == -1)
cout << "NO" << endl;
else
cout << "YES" << endl << 1 << " " << ans << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
inline void solve() {
int n, m, temp;
cin >> n >> m;
vector<long long> inp1(1001, -1), inp2(1001, -1);
for (int i = 0; i < n; i++) cin >> temp, inp1[temp]++;
for (int i = 0; i < m; i++) cin >> temp, inp2[temp]++;
int ans, num = 0;
for (int i = 0; i < 1001; i++) {
if (inp1[i] != -1 && inp2[i] != -1) {
num++, ans = i;
}
}
if (num)
cout << "YES\n1" << ' ' << ans << "\n";
else
cout << "NO\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;
cin >> t;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long maxn = 3e3;
const long long mod = 1e9 + 7;
const long double PI = acos((long double)-1);
long long pw(long long a, long long b, long long md = mod) {
long long res = 1;
while (b) {
if (b & 1) {
res = (a * res) % md;
}
a = (a * a) % md;
b >>= 1;
}
return (res);
}
int q;
int n, m;
int a[maxn], b[maxn];
int cnt1[maxn], cnt2[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> q;
while (q--) {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) cin >> b[i];
for (int i = 0; i < n; i++) cnt1[a[i]] = 1;
for (int j = 0; j < m; j++) cnt2[b[j]] = 1;
int ans = -1;
for (int i = 0; i < 3e3; i++)
if (cnt1[i] and cnt2[i]) ans = i;
memset(cnt1, 0, sizeof cnt1);
;
memset(cnt2, 0, sizeof cnt2);
;
if (ans == -1)
cout << "NO";
else
cout << "YES" << '\n' << 1 << ' ' << ans;
cout << '\n';
}
return (0);
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n, m;
cin >> n >> m;
vector<long long> a(n), b(m);
for (long long &x : a) cin >> x;
for (long long &x : b) cin >> x;
vector<bool> vis(1005, 0);
for (long long x : a) vis[x] = 1;
for (long long y : b) {
if (vis[y]) {
cout << "YES\n1 " << y << "\n";
return;
}
}
cout << "NO\n";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
long long tc = 1;
cin >> tc;
while (tc--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n;
long long m;
cin >> n >> m;
set<long long> st1;
set<long long> st2;
for (long long i = 1; i <= n; i++) {
long long k;
cin >> k;
st1.insert(k);
}
for (long long i = 1; i <= m; i++) {
long long k;
cin >> k;
st2.insert(k);
}
long long flag = 0;
long long ans;
for (auto itr = st1.begin(); itr != st1.end(); itr++) {
if (st2.find(*itr) != st2.end()) {
flag = 1;
ans = *itr;
break;
}
}
if (flag == 0)
cout << "NO" << endl;
else
cout << "YES" << endl << "1 " << ans << endl;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long t;
cin >> t;
while (t--) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
int a[n], b[m];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> b[i];
}
int f = 0, ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i] == b[j]) {
f = 1;
ans = a[i];
break;
}
}
if (f == 1) {
break;
}
}
if (f == 0) {
cout << "NO";
} else {
cout << "YES" << endl;
cout << "1"
<< " " << ans;
}
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int t, n, m, b, a[N];
bool y[N];
int main() {
cin >> t;
while (t--) {
cin >> n >> m;
int e = -1;
for (int i = 0; i < n; i++) {
cin >> a[i];
y[a[i]] = true;
}
for (int i = 0; i < m; i++) {
cin >> b;
if (y[b]) e = b;
}
for (int i = 0; i < n; i++) y[a[i]] = false;
if (e == -1)
cout << "NO\n";
else
cout << "YES\n1 " << e << " "
<< "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, m, check = 0;
cin >> n >> m;
int a[n], b[m];
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 0; i < m; ++i) cin >> b[i];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (a[i] == b[j]) {
cout << "YES\n";
cout << 1 << " " << a[i] << endl;
check++;
break;
}
}
if (check == 1) break;
}
if (check != 1) cout << "NO\n";
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize("Ofast")
using namespace std;
namespace Base {
inline char gc(void) {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
template <class T>
inline void read(T &x) {
T flag = (T)1;
x = 0;
static char ch = gc();
for (; ch > '9' || ch < '0'; ch = gc()) flag = ch == '-' ? -1 : 1;
for (; ch >= '0' && ch <= '9'; ch = gc()) x = (x << 1) + (x << 3) + (ch & 15);
x *= flag;
return;
}
inline void readstr(string &x) {
x = "";
static char ch;
while (isspace(ch = gc()))
;
while (x += ch, !isspace(ch = gc()))
;
}
inline void readstr(char *s) {
do *s = gc();
while ((*s == ' ') || (*s == '\n') || (*s == '\r'));
do *(++s) = gc();
while ((~*s) && (*s != ' ') && (*s != '\n') && (*s != '\r'));
*s = 0;
return;
}
inline void printstr(string x, int num = 0, char ch = '\n') {
for (int i = num; i < x.size(); ++i) putchar(x[i]);
putchar(ch);
}
inline void readch(char &x) {
while (isspace(x = gc()))
;
}
char pf[100000], *o1 = pf, *o2 = pf + 100000;
template <class T>
inline void println(T x, char c = '\n') {
if (x < 0)
(o1 == o2 ? fwrite(pf, 1, 100000, stdout), *(o1 = pf)++ = 45 : *o1++ = 45),
x = -x;
static char s[15], *b;
b = s;
if (!x) *b++ = 48;
for (; x; *b++ = x % 10 + 48, x /= 10)
;
for (; b-- != s; (o1 == o2 ? fwrite(pf, 1, 100000, stdout),
*(o1 = pf)++ = *b : *o1++ = *b))
;
(o1 == o2 ? fwrite(pf, 1, 100000, stdout), *(o1 = pf)++ = c : *o1++ = c);
}
int wbuf[25], _wl = 0;
template <class T>
inline void write(T x) {
if (x == 0) {
putchar(48);
return;
}
if (x < 0) putchar('-'), x = -x;
_wl = 0;
while (x) wbuf[++_wl] = x % 10, x /= 10;
for (int i = _wl; i >= 1; i--) putchar(wbuf[i] + 48);
}
template <class T>
inline void writeln(T x) {
write(x);
puts("");
}
template <class T>
inline void writeln(T x, char c) {
write(x);
putchar(c);
}
template <class T>
inline void writeln(char c, T x) {
putchar(c);
write(x);
}
template <class T>
inline void chkmax(T &x, const T y) {
x > y ? x = x : x = y;
}
template <class T>
inline void chkmin(T &x, const T y) {
x < y ? x = x : x = y;
}
template <class T>
inline T max(const T &x, const T &y, const T &z) {
return x > y ? (x > z ? x : z) : (y > z ? y : z);
}
inline void file(string str) {
freopen((str + ".in").c_str(), "r", stdin);
freopen((str + ".out").c_str(), "w", stdout);
}
struct Vector {
double x, y;
Vector(double _x = 0, double _y = 0) : x(_x), y(_y) {}
inline Vector Vary(void) { return Vector(x, -y); }
inline bool operator<(const Vector &rhs) const {
return x == rhs.x ? y < rhs.y : x < rhs.x;
}
inline Vector operator-(const Vector &rhs) const {
return Vector(x - rhs.x, y - rhs.y);
}
inline Vector operator+(const Vector &rhs) const {
return Vector(x + rhs.x, y + rhs.y);
}
inline Vector operator*(const double &rhs) const {
return Vector(x * rhs, y * rhs);
}
inline Vector operator/(const double &rhs) const {
return Vector(x / rhs, y / rhs);
}
inline Vector operator*(const Vector &rhs) const {
return Vector(x * rhs.x - y * rhs.y, x * rhs.y + y * rhs.x);
}
};
} // namespace Base
using namespace Base;
const int N = 1e6 + 100;
int x[N], y[N], w[N], s[N], v[N];
bool in[N * 2];
vector<pair<int, int> > e[N];
vector<int> res;
int n, m;
int main() {
read(n);
read(m);
for (register int i = (1); i <= (m); i++) {
read(x[i]);
read(y[i]);
w[i] = 1;
s[x[i]]++;
s[y[i]]++;
e[max(x[i], y[i])].push_back(make_pair(min(x[i], y[i]), i));
}
for (register int i = (1); i <= (n); i++) {
for (register int it = (0); it < (int(e[i].size())); it++) {
if (!v[e[i][it].first]) {
v[e[i][it].first] = 1;
w[e[i][it].second] = 0;
s[i]--;
}
in[s[e[i][it].first]] = 1;
}
for (register int it = (0); it < (int(e[i].size())); it++) {
if (!in[s[i]]) break;
s[i]++;
v[e[i][it].first] = 0;
w[e[i][it].second]++;
}
for (register int it = (0); it < (int(e[i].size())); it++)
in[s[e[i][it].first]] = 0;
}
for (register int i = (1); i <= (n); i++)
if (v[i]) res.push_back(i);
writeln(int(res.size()));
for (register int it = (0); it < (int(res.size())); it++)
writeln(res[it], ' ');
puts("");
for (register int i = (1); i <= (m); i++) {
writeln(x[i], ' ');
writeln(y[i], ' ');
writeln(w[i]);
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
inline char gc(void) {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
template <class T>
inline void read(T &x) {
T f = 1;
x = 0;
static char c = gc();
for (; !isdigit(c); c = gc())
if (c == '-') f = -f;
for (; isdigit(c); c = gc()) x = (x << 1) + (x << 3) + (c & 15);
x *= f;
}
inline void readstr(char *s) {
do *s = gc();
while ((*s == ' ') || (*s == '\n') || (*s == '\r'));
do *(++s) = gc();
while ((~*s) && (*s != ' ') && (*s != '\n') && (*s != '\r'));
*s = 0;
return;
}
inline void readch(char &x) {
while (isspace(x = gc()))
;
}
char pf[100000], *o1 = pf, *o2 = pf + 100000;
template <class T>
inline void writeln(T x, char c = '\n') {
if (x < 0)
(o1 == o2 ? fwrite(pf, 1, 100000, stdout), *(o1 = pf)++ = 45 : *o1++ = 45),
x = -x;
static char s[15], *b;
b = s;
if (!x) *b++ = 48;
for (; x; *b++ = x % 10 + 48, x /= 10)
;
for (; b-- != s; (o1 == o2 ? fwrite(pf, 1, 100000, stdout),
*(o1 = pf)++ = *b : *o1++ = *b))
;
(o1 == o2 ? fwrite(pf, 1, 100000, stdout), *(o1 = pf)++ = c : *o1++ = c);
}
template <class T>
inline void chkmax(T &x, const T y) {
x > y ? x = x : x = y;
}
template <class T>
inline void chkmin(T &x, const T y) {
x < y ? x = x : x = y;
}
const int N = 1e6 + 5;
int x[N], y[N], w[N], s[N], v[N];
bool in[N << 1];
vector<pair<int, int> > e[N];
vector<int> res;
int n, m;
int main() {
read(n), read(m);
for (register int i = (1); i <= (m); i++) {
read(x[i]), read(y[i]);
w[i] = 1;
s[x[i]]++;
s[y[i]]++;
e[max(x[i], y[i])].push_back(make_pair(min(x[i], y[i]), i));
}
for (register int i = (1); i <= (n); i++) {
for (auto it : e[i]) {
if (!v[it.first]) {
v[it.first] = 1;
w[it.second] = 0;
s[i]--;
}
in[s[it.first]] = 1;
}
for (auto it : e[i]) {
if (!in[s[i]]) break;
s[i]++;
v[it.first] = 0;
w[it.second]++;
}
for (auto it : e[i]) in[s[it.first]] = 0;
}
for (register int i = (1); i <= (n); i++)
if (v[i]) res.push_back(i);
writeln(int(res.size()));
for (auto it : res) writeln(it, ' ');
puts("");
for (register int i = (1); i <= (m); i++)
writeln(x[i], ' '), writeln(y[i], ' '), writeln(w[i]);
fwrite(pf, 1, o1 - pf, stdout);
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
template <typename T>
void ckmn(T& a, T b) {
a = min(a, b);
}
template <typename T>
void ckmx(T& a, T b) {
a = max(a, b);
}
void rd(int& x) { scanf("%i", &x); }
void rd(long long& x) { scanf("%lld", &x); }
void rd(char* x) { scanf("%s", x); }
void rd(double& x) { scanf("%lf", &x); }
void rd(string& x) { scanf("%s", &x); }
template <typename T1, typename T2>
void rd(pair<T1, T2>& x) {
rd(x.first);
rd(x.second);
}
template <typename T>
void rd(vector<T>& x) {
for (T& i : x) rd(i);
}
template <typename T, typename... A>
void rd(T& x, A&... args) {
rd(x);
rd(args...);
}
template <typename T>
void rd() {
T x;
rd(x);
return x;
}
int ri() {
int x;
rd(x);
return x;
}
template <typename T>
vector<T> rv(int n) {
vector<T> x(n);
rd(x);
return x;
}
template <typename T>
void ra(T a[], int n, int st = 1) {
for (int i = 0; i < n; ++i) rd(a[st + i]);
}
template <typename T1, typename T2>
void ra(T1 a[], T2 b[], int n, int st = 1) {
for (int i = 0; i < n; ++i) rd(a[st + i]), rd(b[st + i]);
}
template <typename T1, typename T2, typename T3>
void ra(T1 a[], T2 b[], T3 c[], int n, int st = 1) {
for (int i = 0; i < n; ++i) rd(a[st + i]), rd(b[st + i]), rd(c[st + i]);
}
void re(vector<int> E[], int m, bool dir = 0) {
for (int i = 0, u, v; i < m; ++i) {
rd(u, v);
E[u].push_back(v);
if (!dir) E[v].push_back(u);
}
}
template <typename T>
void re(vector<pair<int, T>> E[], int m, bool dir = 0) {
for (int i = 0, u, v; i < m; ++i) {
T w;
rd(u, v, w);
E[u].push_back({v, w});
if (!dir) E[v].push_back({u, w});
}
}
const int N = 12550;
const int M = 1000050;
int n, m, u[M], v[M], w[M], val[N], sum[N], ord[N];
vector<pair<int, int>> E[N];
int main() {
rd(n, m);
ra(u, v, m);
for (int i = 1; i <= m; w[i] = 1, i++)
E[u[i]].push_back({v[i], i}), E[v[i]].push_back({u[i], i});
for (int i = 1; i <= n; i++) {
sum[i] = E[i].size();
vector<pair<int, int>> cns;
set<int> bad;
cns.push_back({i, -1});
for (pair<int, int> e : E[i])
if (e.first < i) {
if (!val[e.first]) {
val[e.first] = 1;
w[e.second] = 0;
sum[i]--;
}
cns.push_back(e);
bad.insert(sum[e.first]);
}
while (cns.size()) {
if (!bad.count(sum[i])) {
break;
}
pair<int, int> o = cns.back();
cns.pop_back();
sum[i]++;
if (o.second == -1)
val[i] = 1;
else
val[o.first] = 0, w[o.second]++;
}
}
vector<int> ans;
for (int i = 1; i <= n; i++)
if (val[i]) ans.push_back(i);
printf("%i\n", ans.size());
for (int i : ans) printf("%i ", i);
printf("\n");
for (int i = 1; i <= m; i++) printf("%i %i %i\n", u[i], v[i], w[i]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, u[2100000], v[2100000], c[2100000];
int d[21000], val[21000], vis[4100000];
vector<pair<int, int> > vec[21000];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &u[i], &v[i]);
c[i] = 1;
val[u[i]]++;
val[v[i]]++;
vec[max(u[i], v[i])].push_back(pair<int, int>(min(u[i], v[i]), i));
}
for (int u = 1; u <= n; u++) {
for (pair<int, int> v : vec[u]) {
if (!d[v.first]) {
d[v.first]++;
c[v.second]--;
val[u]--;
}
vis[val[v.first]] = u;
}
for (pair<int, int> v : vec[u]) {
if (vis[val[u]] != u) break;
val[u]++;
d[v.first]--;
c[v.second]++;
}
}
int tot = 0;
for (int i = 1; i <= n; i++)
if (d[i]) tot++;
printf("%d\n", tot);
for (int i = 1; i <= n; i++)
if (d[i]) printf("%d ", i);
putchar('\n');
for (int i = 1; i <= m; i++) printf("%d %d %d\n", u[i], v[i], c[i]);
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
inline char gc(void) {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
template <class T>
inline void read(T &x) {
T f = 1;
x = 0;
static char c = gc();
for (; !isdigit(c); c = gc())
if (c == '-') f = -f;
for (; isdigit(c); c = gc()) x = (x << 1) + (x << 3) + (c & 15);
x *= f;
}
inline void readstr(char *s) {
do *s = gc();
while ((*s == ' ') || (*s == '\n') || (*s == '\r'));
do *(++s) = gc();
while ((~*s) && (*s != ' ') && (*s != '\n') && (*s != '\r'));
*s = 0;
return;
}
inline void readch(char &x) {
while (isspace(x = gc()))
;
}
char pf[100000], *o1 = pf, *o2 = pf + 100000;
template <class T>
inline void writeln(T x, char c = '\n') {
if (x < 0)
(o1 == o2 ? fwrite(pf, 1, 100000, stdout), *(o1 = pf)++ = 45 : *o1++ = 45),
x = -x;
static char s[15], *b;
b = s;
if (!x) *b++ = 48;
for (; x; *b++ = x % 10 + 48, x /= 10)
;
for (; b-- != s; (o1 == o2 ? fwrite(pf, 1, 100000, stdout),
*(o1 = pf)++ = *b : *o1++ = *b))
;
(o1 == o2 ? fwrite(pf, 1, 100000, stdout), *(o1 = pf)++ = c : *o1++ = c);
}
template <class T>
inline void chkmax(T &x, const T y) {
x > y ? x = x : x = y;
}
template <class T>
inline void chkmin(T &x, const T y) {
x < y ? x = x : x = y;
}
const int N = 1e6 + 100;
int x[N], y[N], w[N], s[N], v[N];
bool in[N * 2];
vector<pair<int, int> > e[N];
vector<int> res;
int n, m;
int main() {
read(n), read(m);
for (register int i = (1); i <= (m); i++) {
read(x[i]), read(y[i]);
w[i] = 1;
s[x[i]]++;
s[y[i]]++;
e[max(x[i], y[i])].push_back(make_pair(min(x[i], y[i]), i));
}
for (register int i = (1); i <= (n); i++) {
for (auto it : e[i]) {
if (!v[it.first]) {
v[it.first] = 1;
w[it.second] = 0;
s[i]--;
}
in[s[it.first]] = 1;
}
for (auto it : e[i]) {
if (!in[s[i]]) break;
s[i]++;
v[it.first] = 0;
w[it.second]++;
}
for (auto it : e[i]) in[s[it.first]] = 0;
}
for (register int i = (1); i <= (n); i++)
if (v[i]) res.push_back(i);
writeln(int(res.size()));
for (auto it : res) writeln(it, ' ');
puts("");
for (register int i = (1); i <= (m); i++)
writeln(x[i], ' '), writeln(y[i], ' '), writeln(w[i]);
fwrite(pf, 1, o1 - pf, stdout);
return 0;
}
|
#include <bits/stdc++.h>
const int N = 2e5 + 10, M = 2e6 + 10;
template <typename T = int>
inline T read() {
T x = 0, f = 0;
char ch = getchar();
while (!isdigit(ch)) f |= ch == '-', ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return f ? -x : x;
}
int tot, w[N], vis[N], d[N], u[M], v[M], fir[N], nex[M], got[M], tak[M];
inline void AddEdge(int u, int v) {
nex[++tot] = fir[u], fir[u] = tot, got[tot] = v, tak[tot] = 1;
}
int main() {
int n = read(), m = read();
for (int i = 1; i <= m; ++i) {
int u = read(), v = read();
::u[i] = u, ::v[i] = v;
AddEdge(std::max(u, v), std::min(u, v)), ++d[u], ++d[v];
}
for (int i = 1; i <= n; ++i) w[i] = 0;
for (int u = 1; u <= n; ++u) {
int res = 0;
for (int i = fir[u]; i; i = nex[i])
if (!w[got[i]]) w[got[i]]++, tak[i]--, --d[u];
for (int i = fir[u]; i; i = nex[i]) vis[d[got[i]]] = true;
for (int i = fir[u]; i; i = nex[i]) {
if (!vis[d[u]]) break;
w[got[i]]--, tak[i]++, ++d[u];
}
for (int i = fir[u]; i; i = nex[i]) vis[d[got[i]]] = false;
}
std::vector<int> ans;
for (int i = 1; i <= n; ++i)
if (w[i]) ans.push_back(i);
printf("%d\n", ans.size());
for (auto u : ans) printf("%d ", u);
if (ans.size()) puts("");
for (int i = 1; i <= m; ++i) printf("%d %d %d\n", u[i], v[i], tak[i]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 12500, M = 1000000;
int n, m;
vector<pair<int, int> > nei[N + 1];
int a[M + 1], b[M + 1];
int ansv[N + 1], anse[M + 1];
int val[N + 1];
bool hav[2 * M + N + 1];
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
a[i] = x, b[i] = y;
nei[x].push_back(make_pair(y, i));
nei[y].push_back(make_pair(x, i));
anse[i] = 1;
}
for (int i = 1; i <= n; i++) {
int dec = 0;
for (int j = 0; j < nei[i].size(); j++) {
int x = nei[i][j].first;
if (x < i) hav[val[x]] = true;
if (x < i && !ansv[x]) dec++;
}
for (int j = nei[i].size() - dec;; j++)
if (!hav[j]) {
val[i] = j;
break;
}
int now = val[i] - nei[i].size();
for (int j = 0; j < nei[i].size(); j++) {
int x = nei[i][j].first, y = nei[i][j].second;
if (x < i) hav[val[x]] = false;
if (x < i && !ansv[x] && now < 0) now++, ansv[x]++, anse[y]--;
if (x < i && ansv[x] && now > 0) now--, ansv[x]--, anse[y]++;
}
}
vector<int> v;
for (int i = 1; i <= n; i++)
if (ansv[i]) v.push_back(i);
cout << v.size() << "\n";
for (int i = 0; i < v.size(); i++) cout << v[i] << " \n"[i + 1 == v.size()];
for (int i = 1; i <= m; i++) printf("%d %d %d\n", a[i], b[i], anse[i]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
namespace IO {
const int sz = 1 << 15;
char inbuf[sz], outbuf[sz];
char *pinbuf = inbuf + sz;
char *poutbuf = outbuf;
inline char _getchar() {
if (pinbuf == inbuf + sz) fread(inbuf, 1, sz, stdin), pinbuf = inbuf;
return *(pinbuf++);
}
inline void _putchar(char x) {
if (poutbuf == outbuf + sz) fwrite(outbuf, 1, sz, stdout), poutbuf = outbuf;
*(poutbuf++) = x;
}
inline void flush() {
if (poutbuf != outbuf)
fwrite(outbuf, 1, poutbuf - outbuf, stdout), poutbuf = outbuf;
}
} // namespace IO
inline int read() {
int v = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
v = v * 10 + c - '0';
c = getchar();
}
return v * f;
}
const int Maxn = 12567;
int n, m;
vector<int> G[Maxn];
int R[Maxn], F[Maxn];
vector<pair<pair<int, int>, int> > Ans;
int main() {
n = read();
m = read();
for (int i = 0; i < m; i++) {
int a, b;
a = read();
b = read();
if (a > b) swap(a, b);
G[b].push_back(a);
R[a]++;
R[b]++;
}
for (int i = 2; i <= n; i++) {
set<int> S;
int l = 0, r = 0;
for (int j = 0; j < G[i].size(); j++) {
int x = G[i][j];
S.insert(R[x]);
if (F[x])
r++;
else
l++;
}
int t = -114514;
for (int j = -l; j <= r; j++) {
if (S.find(j + R[i]) == S.end()) {
t = j;
break;
}
}
assert(t != -114514);
int TT = t;
for (int j = 0; j < G[i].size(); j++) {
int x = G[i][j];
if (F[x]) {
if (t > 0) {
F[x] = 0;
Ans.push_back(make_pair(make_pair(i, x), 2));
t--;
} else
Ans.push_back(make_pair(make_pair(i, x), 1));
} else {
if (t < 0) {
F[x] = 1;
Ans.push_back(make_pair(make_pair(i, x), 0));
t++;
} else
Ans.push_back(make_pair(make_pair(i, x), 1));
}
}
R[i] += TT;
}
int s = 0;
for (int i = 1; i <= n; i++) s += F[i];
printf("%d\n", s);
for (int i = 1; i <= n; i++) {
if (F[i]) {
printf("%d ", i);
}
}
printf("\n");
for (int i = 0; i < Ans.size(); i++) {
printf("%d %d %d\n", Ans[i].first.first, Ans[i].first.second,
Ans[i].second);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline char gc(void) {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
template <class T>
inline void read(T &x) {
T f = 1;
x = 0;
static char c = gc();
for (; !isdigit(c); c = gc())
if (c == '-') f = -f;
for (; isdigit(c); c = gc()) x = (x << 1) + (x << 3) + (c & 15);
x *= f;
}
inline void readstr(char *s) {
do *s = gc();
while ((*s == ' ') || (*s == '\n') || (*s == '\r'));
do *(++s) = gc();
while ((~*s) && (*s != ' ') && (*s != '\n') && (*s != '\r'));
*s = 0;
return;
}
inline void readch(char &x) {
while (isspace(x = gc()))
;
}
template <class T>
inline void write(T x) {
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + 48);
}
template <class T>
inline void writeln(T x, char c = '\n') {
write(x);
putchar(c);
}
template <class T>
inline void chkmax(T &x, const T y) {
x > y ? x = x : x = y;
}
template <class T>
inline void chkmin(T &x, const T y) {
x < y ? x = x : x = y;
}
const int N = 1e6 + 100;
int x[N], y[N], w[N], s[N], v[N];
bool in[N * 2];
vector<pair<int, int> > e[N];
vector<int> res;
int n, m;
int main() {
read(n), read(m);
for (int i = (1); i <= (m); i++) {
read(x[i]), read(y[i]);
w[i] = 1;
s[x[i]]++;
s[y[i]]++;
e[max(x[i], y[i])].push_back(make_pair(min(x[i], y[i]), i));
}
for (int i = (1); i <= (n); i++) {
for (auto it : e[i]) {
if (!v[it.first]) {
v[it.first] = 1;
w[it.second] = 0;
s[i]--;
}
in[s[it.first]] = 1;
}
for (auto it : e[i]) {
if (!in[s[i]]) break;
s[i]++;
v[it.first] = 0;
w[it.second]++;
}
for (auto it : e[i]) in[s[it.first]] = 0;
}
for (int i = (1); i <= (n); i++)
if (v[i]) res.push_back(i);
writeln(int(res.size()));
for (auto it : res) writeln(it, ' ');
puts("");
for (int i = (1); i <= (m); i++)
writeln(x[i], ' '), writeln(y[i], ' '), writeln(w[i]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mxn = 2e4 + 4;
const int mxm = 1e6 + 6;
vector<pair<int, int> > g[mxn];
int one[mxn], n, m;
int wei[mxn];
struct edge {
int from, to;
int cap;
};
inline edge me(int from, int to) {
edge rt;
rt.from = from, rt.to = to, rt.cap = 1;
++wei[from], ++wei[to];
return rt;
}
edge e[mxm];
inline void solve() {
scanf("%d%d", &n, &m);
for (register int i = 1; i <= m; ++i) {
register int u, v;
scanf("%d%d", &u, &v);
e[i] = me(u, v);
g[u].push_back(make_pair(v, i));
g[v].push_back(make_pair(u, i));
}
for (int i = 1; i <= n; ++i) {
int cnt = 0;
for (int j = 0; j < g[i].size(); ++j) {
int t = g[i][j].first;
if (t > i) continue;
++cnt;
if (!one[t]) {
int id = g[i][j].second;
--e[id].cap;
one[t] = 1;
--wei[i];
}
}
set<int> s;
s.clear();
for (int j = 0; j <= cnt; ++j) s.insert(j + wei[i]);
for (int j = 0; j < g[i].size(); ++j) {
int t = g[i][j].first;
if (t > i) continue;
if (s.find(wei[t]) != s.end()) s.erase(wei[t]);
}
int ans = *s.begin();
int need = ans - wei[i];
for (int j = 0; j < g[i].size() and need; ++j) {
int t = g[i][j].first, id = g[i][j].second;
if (t > i) continue;
one[t] = 0;
e[id].cap++;
wei[i]++;
--need;
}
}
vector<int> ones;
for (int i = 1; i <= n; ++i)
if (one[i]) ones.push_back(i);
printf("%d\n", ones.size());
for (int i = 0; i < ones.size(); ++i) printf("%d ", ones[i]);
if (ones.size()) printf("\n");
for (int i = 1; i <= m; ++i)
printf("%d %d %d\n", e[i].from, e[i].to, e[i].cap);
}
int main() {
ios_base::sync_with_stdio(false);
int T = 1;
for (; T--;) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 12550;
const int M = 1000050;
int n, m, u[M], v[M], w[M], val[N], sum[N], ord[N];
vector<pair<int, int> > E[N];
int main() {
scanf("%i %i", &n, &m);
for (int i = 1; i <= m; i++) scanf("%i %i", &u[i], &v[i]);
for (int i = 1; i <= m; w[i] = 1, i++)
E[u[i]].push_back({v[i], i}), E[v[i]].push_back({u[i], i});
for (int i = 1; i <= n; i++) {
sum[i] = E[i].size();
vector<pair<int, int> > cns;
set<int> bad;
cns.push_back({i, -1});
for (pair<int, int> e : E[i])
if (e.first < i) {
if (!val[e.first]) {
val[e.first] = 1;
w[e.second] = 0;
sum[i]--;
}
cns.push_back(e);
bad.insert(sum[e.first]);
}
while (cns.size()) {
if (!bad.count(sum[i])) {
break;
}
pair<int, int> o = cns.back();
cns.pop_back();
sum[i]++;
if (o.second == -1)
val[i] = 1;
else
val[o.first] = 0, w[o.second]++;
}
}
vector<int> ans;
for (int i = 1; i <= n; i++)
if (val[i]) ans.push_back(i);
printf("%i\n", ans.size());
for (int i : ans) printf("%i ", i);
printf("\n");
for (int i = 1; i <= m; i++) printf("%i %i %i\n", u[i], v[i], w[i]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
namespace Base {
inline char gc(void) {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
template <class T>
inline void read(T &x) {
T flag = (T)1;
x = 0;
static char ch = getchar();
for (; ch > '9' || ch < '0'; ch = getchar()) flag = ch == '-' ? -1 : 1;
for (; ch >= '0' && ch <= '9'; ch = getchar())
x = (x << 1) + (x << 3) + (ch & 15);
x *= flag;
return;
}
inline void readstr(string &x) {
x = "";
static char ch;
while (isspace(ch = getchar()))
;
while (x += ch, !isspace(ch = getchar()))
;
}
inline void readstr(char *s) {
do *s = getchar();
while ((*s == ' ') || (*s == '\n') || (*s == '\r'));
do *(++s) = getchar();
while ((~*s) && (*s != ' ') && (*s != '\n') && (*s != '\r'));
*s = 0;
return;
}
inline void printstr(string x, int num = 0, char ch = '\n') {
for (int i = num; i < x.size(); ++i) putchar(x[i]);
putchar(ch);
}
inline void readch(char &x) {
while (isspace(x = getchar()))
;
}
char pf[100000], *o1 = pf, *o2 = pf + 100000;
template <class T>
inline void println(T x, char c = '\n') {
if (x < 0)
(o1 == o2 ? fwrite(pf, 1, 100000, stdout), *(o1 = pf)++ = 45 : *o1++ = 45),
x = -x;
static char s[15], *b;
b = s;
if (!x) *b++ = 48;
for (; x; *b++ = x % 10 + 48, x /= 10)
;
for (; b-- != s; (o1 == o2 ? fwrite(pf, 1, 100000, stdout),
*(o1 = pf)++ = *b : *o1++ = *b))
;
(o1 == o2 ? fwrite(pf, 1, 100000, stdout), *(o1 = pf)++ = c : *o1++ = c);
}
int wbuf[25], _wl = 0;
template <class T>
inline void write(T x) {
if (x == 0) {
putchar(48);
return;
}
if (x < 0) putchar('-'), x = -x;
_wl = 0;
while (x) wbuf[++_wl] = x % 10, x /= 10;
for (int i = _wl; i >= 1; i--) putchar(wbuf[i] + 48);
}
template <class T>
inline void writeln(T x) {
write(x);
puts("");
}
template <class T>
inline void writeln(T x, char c) {
write(x);
putchar(c);
}
template <class T>
inline void writeln(char c, T x) {
putchar(c);
write(x);
}
template <class T>
inline void chkmax(T &x, const T y) {
x > y ? x = x : x = y;
}
template <class T>
inline void chkmin(T &x, const T y) {
x < y ? x = x : x = y;
}
template <class T>
inline T max(const T &x, const T &y, const T &z) {
return x > y ? (x > z ? x : z) : (y > z ? y : z);
}
inline void file(string str) {
freopen((str + ".in").c_str(), "r", stdin);
freopen((str + ".out").c_str(), "w", stdout);
}
struct Vector {
double x, y;
Vector(double _x = 0, double _y = 0) : x(_x), y(_y) {}
inline Vector Vary(void) { return Vector(x, -y); }
inline bool operator<(const Vector &rhs) const {
return x == rhs.x ? y < rhs.y : x < rhs.x;
}
inline Vector operator-(const Vector &rhs) const {
return Vector(x - rhs.x, y - rhs.y);
}
inline Vector operator+(const Vector &rhs) const {
return Vector(x + rhs.x, y + rhs.y);
}
inline Vector operator*(const double &rhs) const {
return Vector(x * rhs, y * rhs);
}
inline Vector operator/(const double &rhs) const {
return Vector(x / rhs, y / rhs);
}
inline Vector operator*(const Vector &rhs) const {
return Vector(x * rhs.x - y * rhs.y, x * rhs.y + y * rhs.x);
}
};
} // namespace Base
using namespace Base;
const int N = 1e6 + 100;
int x[N], y[N], w[N], s[N], v[N];
bool in[N * 2];
vector<pair<int, int> > e[N];
vector<int> res;
int main() {
int n, m;
read(n);
read(m);
for (int i = (1); i <= (m); i++) {
read(x[i]);
read(y[i]);
w[i] = 1;
s[x[i]]++;
s[y[i]]++;
e[max(x[i], y[i])].push_back(make_pair(min(x[i], y[i]), i));
}
for (int i = (1); i <= (n); i++) {
for (int it = (0); it <= (int(e[i].size()) - 1); it++) {
if (!v[e[i][it].first]) {
v[e[i][it].first] = 1;
w[e[i][it].second] = 0;
s[i]--;
}
in[s[e[i][it].first]] = 1;
}
for (int it = (0); it <= (int(e[i].size()) - 1); it++) {
if (!in[s[i]]) break;
s[i]++;
v[e[i][it].first] = 0;
w[e[i][it].second]++;
}
for (int it = (0); it <= (int(e[i].size()) - 1); it++)
in[s[e[i][it].first]] = 0;
}
for (int i = (1); i <= (n); i++)
if (v[i]) res.push_back(i);
writeln(int(res.size()));
for (int it = (0); it <= (int(res.size()) - 1); it++) writeln(res[it], ' ');
puts("");
for (int i = (1); i <= (m); i++) {
writeln(x[i], ' ');
writeln(y[i], ' ');
writeln(w[i]);
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
inline char gc(void) {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
template <class T>
inline void read(T &x) {
T f = 1;
x = 0;
static char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c & 15);
x *= f;
}
inline void readstr(char *s) {
do *s = getchar();
while ((*s == ' ') || (*s == '\n') || (*s == '\r'));
do *(++s) = getchar();
while ((~*s) && (*s != ' ') && (*s != '\n') && (*s != '\r'));
*s = 0;
return;
}
inline void readch(char &x) {
while (isspace(x = getchar()))
;
}
template <class T>
inline void write(T x) {
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + 48);
}
template <class T>
inline void writeln(T x, char c = '\n') {
write(x);
putchar(c);
}
template <class T>
inline void chkmax(T &x, const T y) {
x > y ? x = x : x = y;
}
template <class T>
inline void chkmin(T &x, const T y) {
x < y ? x = x : x = y;
}
const int N = 1e6 + 100;
int x[N], y[N], w[N], s[N], v[N];
bool in[N * 2];
vector<pair<int, int> > e[N];
vector<int> res;
int n, m;
int main() {
read(n), read(m);
for (int i = (1); i <= (m); i++) {
read(x[i]), read(y[i]);
w[i] = 1;
s[x[i]]++;
s[y[i]]++;
e[max(x[i], y[i])].push_back(make_pair(min(x[i], y[i]), i));
}
for (int i = (1); i <= (n); i++) {
for (auto it : e[i]) {
if (!v[it.first]) {
v[it.first] = 1;
w[it.second] = 0;
s[i]--;
}
in[s[it.first]] = 1;
}
for (auto it : e[i]) {
if (!in[s[i]]) break;
s[i]++;
v[it.first] = 0;
w[it.second]++;
}
for (auto it : e[i]) in[s[it.first]] = 0;
}
for (int i = (1); i <= (n); i++)
if (v[i]) res.push_back(i);
writeln(int(res.size()));
for (auto it : res) writeln(it, ' ');
puts("");
for (int i = (1); i <= (m); i++)
writeln(x[i], ' '), writeln(y[i], ' '), writeln(w[i]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100;
int x[N], y[N], w[N], s[N], v[N];
bool in[N * 2];
vector<pair<int, int> > e[N];
vector<int> res;
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = (1); i <= (m); i++) {
scanf("%d%d", x + i, y + i);
w[i] = 1;
s[x[i]]++;
s[y[i]]++;
e[max(x[i], y[i])].push_back(make_pair(min(x[i], y[i]), i));
}
for (int i = (1); i <= (n); i++) {
for (auto it : e[i]) {
if (!v[it.first]) {
v[it.first] = 1;
w[it.second] = 0;
s[i]--;
}
in[s[it.first]] = 1;
}
for (auto it : e[i]) {
if (!in[s[i]]) break;
s[i]++;
v[it.first] = 0;
w[it.second]++;
}
for (auto it : e[i]) in[s[it.first]] = 0;
}
for (int i = (1); i <= (n); i++)
if (v[i]) res.push_back(i);
printf("%d\n", int(res.size()));
for (auto it : res) printf("%d ", it);
puts("");
for (int i = (1); i <= (m); i++) printf("%d %d %d\n", x[i], y[i], w[i]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
scanf("%i %i", &n, &m);
vector<vector<int> > graf(n);
vector<int> par(n), coins(n);
for (int i = 0; i < m; i++) {
int u, v;
scanf("%i %i", &u, &v);
u--;
v--;
graf[max(u, v)].push_back(min(u, v));
coins[u]++;
coins[v]++;
}
vector<pair<pair<int, int>, int> > edgeSol;
for (int i = 0; i < n; i++) {
map<int, int> taken;
vector<int> up, down;
for (auto p : graf[i]) {
taken[coins[p]] = 1;
if (par[p])
up.push_back(p);
else
down.push_back(p);
}
if (!taken[coins[i]]) {
for (auto p : up) edgeSol.push_back({{p, i}, 1});
for (auto p : down) edgeSol.push_back({{p, i}, 1});
continue;
}
bool nasao = 0;
for (int j = 1; j <= up.size(); j++) {
if (!taken[coins[i] + j]) {
coins[i] += j;
for (auto p : down) edgeSol.push_back({{p, i}, 1});
for (int k = 0; k < up.size(); k++) {
if (k < j)
edgeSol.push_back({{up[k], i}, 2}), par[up[k]] ^= 1;
else
edgeSol.push_back({{up[k], i}, 1});
}
nasao = 1;
break;
}
}
if (!nasao) {
for (int j = 1; j <= down.size(); j++) {
if (!taken[coins[i] - j]) {
coins[i] -= j;
for (auto p : up) edgeSol.push_back({{p, i}, 1});
for (int k = 0; k < down.size(); k++) {
if (k < j)
edgeSol.push_back({{down[k], i}, 0}), par[down[k]] ^= 1;
else
edgeSol.push_back({{down[k], i}, 1});
}
nasao = 1;
break;
}
}
}
assert(nasao);
}
vector<int> lst;
for (int i = 0; i < n; i++)
if (par[i]) lst.push_back(i);
printf("%i ", lst.size());
for (auto p : lst) printf("%i ", p + 1);
printf("\n");
for (auto p : edgeSol)
printf("%i %i %i\n", p.first.first + 1, p.first.second + 1, p.second);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int n, m, x[N], y[N], w[N], s[N], v[N], a[N];
vector<pair<int, int>> e[N];
vector<int> r;
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &x[i], &y[i]);
w[i] = 1, s[x[i]]++, s[y[i]]++;
if (x[i] > y[i]) swap(x[i], y[i]);
e[y[i]].emplace_back(x[i], i);
}
for (int i = 1; i <= n; i++) {
for (auto j : e[i]) {
int p = j.first, q = j.second;
if (!v[p]) {
v[p] = 1;
w[q] = 0;
s[i]--;
}
a[s[p]] = 1;
}
for (auto j : e[i]) {
int p = j.first, q = j.second;
if (!a[s[i]]) break;
s[i]++;
v[p] = 0;
w[q]++;
}
for (auto j : e[i]) a[s[j.first]] = 0;
}
for (int i = 1; i <= n; i++)
if (v[i]) r.push_back(i);
printf("%d\n", r.size());
for (auto i : r) printf("%d ", i);
if (r.size()) puts("");
for (int i = 1; i <= m; i++) printf("%d %d %d\n", x[i], y[i], w[i]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1.3e4 + 5;
const int MAXM = 1e6 + 5;
struct Edge {
int next, to;
} e[MAXM << 1];
int head[MAXN], etot = 0;
inline void add(int u, int v) {
e[++etot] = (Edge){head[u], v};
head[u] = etot;
}
int sum[MAXN], val[MAXN], ew[MAXM];
int main(void) {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
for (int i = 1; i <= m; ++i) ew[i] = 1;
for (int i = 1; i <= etot; ++i) ++sum[e[i].to];
for (int u = 1; u <= n; ++u) {
vector<int> posi, nega;
static bool has[MAXM * 2];
for (int i = head[u]; i; i = e[i].next)
if (e[i].to < u) {
int v = e[i].to;
has[sum[v]] = 1;
if (val[v])
posi.push_back(i);
else
nega.push_back(i);
}
int use = 0;
for (int i = -(int)nega.size(); i <= (int)posi.size(); ++i)
if (!has[sum[u] + i]) {
use = i;
break;
}
sum[u] += use;
if (use > 0) {
for (int j = 0; j < use; ++j) {
int eid = posi[j];
ew[(eid + 1) >> 1] = 2;
val[e[eid].to] = 0;
}
} else if (use < 0) {
use = -use;
for (int j = 0; j < use; ++j) {
int eid = nega[j];
ew[(eid + 1) >> 1] = 0;
val[e[eid].to] = 1;
}
}
for (int i = head[u]; i; i = e[i].next)
if (e[i].to < i) has[sum[e[i].to]] = 0;
}
int keep = 0;
for (int i = 1; i <= n; ++i)
if (val[i]) ++keep;
printf("%d\n", keep);
for (int i = 1; i <= n; ++i)
if (val[i]) printf("%d ", i);
putchar('\n');
for (int i = 1; i <= m; ++i)
printf("%d %d %d\n", e[i << 1].to, e[(i << 1) - 1].to, ew[i]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int x[N], y[N], w[N], s[N], v[N];
vector<pair<int, int>> e[N];
vector<int> ans;
int n, m;
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> x[i] >> y[i];
w[i] = 1;
++s[x[i]];
++s[y[i]];
e[max(x[i], y[i])].push_back({min(x[i], y[i]), i});
}
for (int i = 1; i <= n; i++) {
set<int> st;
for (auto &j : e[i]) {
if (!v[j.first]) v[j.first] = 1, w[j.second] = 0, --s[i];
st.insert(s[j.first]);
}
for (auto &j : e[i]) {
if (!st.count(s[i])) break;
++s[i];
v[j.first] = 0;
++w[j.second];
}
}
for (int i = 1; i <= n; i++)
if (v[i]) ans.push_back(i);
cout << ans.size() << endl;
for (auto &i : ans) cout << i << ' ';
cout << endl;
for (int i = 1; i <= m; i++)
cout << x[i] << ' ' << y[i] << ' ' << w[i] << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline char gc(void) {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
template <class T>
inline void read(T &x) {
T f = 1;
x = 0;
static char c = gc();
for (; !isdigit(c); c = gc())
if (c == '-') f = -f;
for (; isdigit(c); c = gc()) x = (x << 1) + (x << 3) + (c & 15);
x *= f;
}
inline void readstr(char *s) {
do *s = gc();
while ((*s == ' ') || (*s == '\n') || (*s == '\r'));
do *(++s) = gc();
while ((~*s) && (*s != ' ') && (*s != '\n') && (*s != '\r'));
*s = 0;
return;
}
inline void readch(char &x) {
while (isspace(x = gc()))
;
}
char pf[100000], *o1 = pf, *o2 = pf + 100000;
template <class T>
inline void writeln(T x, char c = '\n') {
if (x < 0)
(o1 == o2 ? fwrite(pf, 1, 100000, stdout), *(o1 = pf)++ = 45 : *o1++ = 45),
x = -x;
static char s[15], *b;
b = s;
if (!x) *b++ = 48;
for (; x; *b++ = x % 10 + 48, x /= 10)
;
for (; b-- != s; (o1 == o2 ? fwrite(pf, 1, 100000, stdout),
*(o1 = pf)++ = *b : *o1++ = *b))
;
(o1 == o2 ? fwrite(pf, 1, 100000, stdout), *(o1 = pf)++ = c : *o1++ = c);
}
template <class T>
inline void chkmax(T &x, const T y) {
x > y ? x = x : x = y;
}
template <class T>
inline void chkmin(T &x, const T y) {
x < y ? x = x : x = y;
}
const int N = 1e6 + 100;
int x[N], y[N], w[N], s[N], v[N];
bool in[N * 2];
vector<pair<int, int> > e[N];
vector<int> res;
int n, m;
int main() {
read(n), read(m);
for (register int i = (1); i <= (m); i++) {
read(x[i]), read(y[i]);
w[i] = 1;
s[x[i]]++;
s[y[i]]++;
e[max(x[i], y[i])].push_back(make_pair(min(x[i], y[i]), i));
}
for (register int i = (1); i <= (n); i++) {
for (auto it : e[i]) {
if (!v[it.first]) {
v[it.first] = 1;
w[it.second] = 0;
s[i]--;
}
in[s[it.first]] = 1;
}
for (auto it : e[i]) {
if (!in[s[i]]) break;
s[i]++;
v[it.first] = 0;
w[it.second]++;
}
for (auto it : e[i]) in[s[it.first]] = 0;
}
for (register int i = (1); i <= (n); i++)
if (v[i]) res.push_back(i);
writeln(int(res.size()));
for (auto it : res) writeln(it, ' ');
puts("");
for (register int i = (1); i <= (m); i++)
writeln(x[i], ' '), writeln(y[i], ' '), writeln(w[i]);
fwrite(pf, 1, o1 - pf, stdout);
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.