text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 7; const int N = 1e2 + 10; int main() { string key = "RRRRPSSPRR", st, s, t; for (int i = 0; i < 10; i++) { cout << key[i] << endl; getline(cin, st); s += st == "ai" ? '0' : '1'; } if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "1111000011") t = "RRRRRRRRRR"; if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0000000110") t = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { cout << t[i] << endl; getline(cin, st); } return 0; }
#include <bits/stdc++.h> using namespace std; int modInverse(int a, int m); int gcd(int a, int b); int power(int x, unsigned int y, unsigned int m); void pairsort(int a[], int b[], int n); int logint(int x, int y); int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } int power(int x, unsigned int y, unsigned int m) { if (y == 0) return 1; int p = power(x, y / 2, m) % m; p = (p * p) % m; return (y % 2 == 0) ? p : (x * p) % m; } int modInverse(int a, int m) { int m0 = m; int y = 0, x = 1; if (m == 1) return 0; while (a > 1) { int q = a / m; int t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } void pairsort(int a[], int b[], int n) { pair<int, int> pairt[n]; for (int i = 0; i < n; i++) { pairt[i].first = a[i]; pairt[i].second = b[i]; } sort(pairt, pairt + n); for (int i = 0; i < n; i++) { a[i] = pairt[i].first; b[i] = pairt[i].second; } } int logint(int x, int y) { int ans = 0; int a = 1; for (int i = 0; i < x; i++) { if (x <= a) { return ans; } ans++; a *= y; } return -1; } int xd(char c) { cout << c << endl; string s; cin >> s; if (s == "player") return 1; else return 0; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); char c[3] = {'R', 'P', 'S'}; int r[10]; r[0] = xd('R'); r[1] = xd('R'); r[2] = xd('P'); r[3] = xd('P'); r[4] = xd('S'); r[5] = xd('S'); r[6] = xd('P'); r[7] = xd('P'); r[8] = xd('R'); r[9] = xd('R'); if (r[0] == 0 && r[1] == 0 && r[2] == 1 && r[3] == 1 && r[4] == 0 && r[5] == 0 && r[6] == 1 && r[7] == 1 && r[8] == 0 && r[9] == 0) { for (int i = 0; i < 10; i++) { xd(c[1]); } } else if (r[0] == 0 && r[1] == 0 && r[2] == 0 && r[3] == 0 && r[4] == 1 && r[5] == 1 && r[6] == 0 && r[7] == 0 && r[8] == 0 && r[9] == 0) { for (int i = 0; i < 10; i++) { xd(c[2]); } } else if (r[0] == 1 && r[1] == 1 && r[2] == 0 && r[3] == 0 && r[4] == 0 && r[5] == 0 && r[6] == 0 && r[7] == 0 && r[8] == 1 && r[9] == 1) { for (int i = 0; i < 10; i++) { xd(c[0]); } } else if (r[0] == 0 && r[1] == 0 && r[2] == 0 && r[3] == 1 && r[4] == 1 && r[5] == 0 && r[6] == 1 && r[7] == 0 && r[8] == 1 && r[9] == 0) { int t = 2; for (int i = 0; i < 10; i++) { xd(c[t]); t++; t %= 3; } } else if (r[1] == 0 && r[2] == 1 && r[3] == 0 && r[4] == 1 && r[5] == 0 && r[6] == 0 && r[7] == 0 && r[8] == 0 && r[9] == 0) { int t = 1; for (int i = 0; i < 10; i++) { xd(c[t]); t++; t %= 3; } } else if (r[1] == 0 && r[2] == 0 && r[3] == 0 && r[4] == 0 && r[5] == 0 && r[6] == 1 && r[7] == 0 && r[8] == 1 && r[9] == 0) { int t = 2; for (int i = 0; i < 10; i++) { xd(c[t]); t += 2; t %= 3; } } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename... As> struct tpl : public std::tuple<As...> { using std::tuple<As...>::tuple; tpl() {} tpl(std::tuple<As...> const& b) { std::tuple<As...>::operator=(b); } template <typename T = tuple<As...> > typename tuple_element<0, T>::type const& x() const { return get<0>(*this); } template <typename T = tuple<As...> > typename tuple_element<0, T>::type& x() { return get<0>(*this); } template <typename T = tuple<As...> > typename tuple_element<1, T>::type const& y() const { return get<1>(*this); } template <typename T = tuple<As...> > typename tuple_element<1, T>::type& y() { return get<1>(*this); } template <typename T = tuple<As...> > typename tuple_element<2, T>::type const& z() const { return get<2>(*this); } template <typename T = tuple<As...> > typename tuple_element<2, T>::type& z() { return get<2>(*this); } template <typename T = tuple<As...> > typename tuple_element<3, T>::type const& w() const { return get<3>(*this); } template <typename T = tuple<As...> > typename tuple_element<3, T>::type& w() { return get<3>(*this); } }; using lli = long long int; using llu = long long unsigned; using pii = tpl<lli, lli>; using piii = tpl<lli, lli, lli>; using piiii = tpl<lli, lli, lli, lli>; using vi = vector<lli>; using vii = vector<pii>; using viii = vector<piii>; using vvi = vector<vi>; using vvii = vector<vii>; using vviii = vector<viii>; template <class T> using min_queue = priority_queue<T, vector<T>, greater<T> >; template <class T> using max_queue = priority_queue<T>; template <size_t... I> struct my_index_sequence { using type = my_index_sequence; static constexpr array<size_t, sizeof...(I)> value = {{I...}}; }; namespace my_index_sequence_detail { template <typename I, typename J> struct concat; template <size_t... I, size_t... J> struct concat<my_index_sequence<I...>, my_index_sequence<J...> > : my_index_sequence<I..., (sizeof...(I) + J)...> {}; template <size_t N> struct make_index_sequence : concat<typename make_index_sequence<N / 2>::type, typename make_index_sequence<N - N / 2>::type>::type {}; template <> struct make_index_sequence<0> : my_index_sequence<> {}; template <> struct make_index_sequence<1> : my_index_sequence<0> {}; } // namespace my_index_sequence_detail template <class... A> using my_index_sequence_for = typename my_index_sequence_detail::make_index_sequence<sizeof...(A)>::type; template <class T, size_t... I> void print_tuple(ostream& s, T const& a, my_index_sequence<I...>) { using swallow = int[]; (void)swallow{0, (void(s << (I == 0 ? "" : ", ") << get<I>(a)), 0)...}; } template <class T> ostream& print_collection(ostream& s, T const& a); template <class... A> ostream& operator<<(ostream& s, tpl<A...> const& a); template <class... A> ostream& operator<<(ostream& s, tuple<A...> const& a); template <class A, class B> ostream& operator<<(ostream& s, pair<A, B> const& a); template <class T, size_t I> ostream& operator<<(ostream& s, array<T, I> const& a) { return print_collection(s, a); } template <class T> ostream& operator<<(ostream& s, vector<T> const& a) { return print_collection(s, a); } template <class T, class U> ostream& operator<<(ostream& s, multimap<T, U> const& a) { return print_collection(s, a); } template <class T> ostream& operator<<(ostream& s, multiset<T> const& a) { return print_collection(s, a); } template <class T, class U> ostream& operator<<(ostream& s, map<T, U> const& a) { return print_collection(s, a); } template <class T> ostream& operator<<(ostream& s, set<T> const& a) { return print_collection(s, a); } template <class T> ostream& print_collection(ostream& s, T const& a) { s << '['; for (auto it = begin(a); it != end(a); ++it) { s << *it; if (it != prev(end(a))) s << " "; } return s << ']'; } template <class... A> ostream& operator<<(ostream& s, tpl<A...> const& a) { s << '('; print_tuple(s, a, my_index_sequence_for<A...>{}); return s << ')'; } template <class... A> ostream& operator<<(ostream& s, tuple<A...> const& a) { s << '('; print_tuple(s, a, my_index_sequence_for<A...>{}); return s << ')'; } template <class A, class B> ostream& operator<<(ostream& s, pair<A, B> const& a) { return s << "(" << get<0>(a) << ", " << get<1>(a) << ")"; } struct strat { virtual ~strat() {} virtual int next() = 0; virtual bool update(int play) = 0; }; struct strat_period : strat { int turn; vi A; strat_period(vi A_) : turn(0), A(A_) {} virtual ~strat_period() {} virtual int next() { return A[turn % A.size()]; } virtual bool update(int i) { int self = next(); turn++; return i == (self + 1) % 3; } }; struct strat_last : strat { int last; int offset; strat_last(int last_, int offset_) : last(last_), offset(offset_) {} virtual ~strat_last() {} virtual int next() { return (last + offset) % 3; } virtual bool update(int i) { int self = next(); last = i; return i == (self + 1) % 3; } }; string RPS = "RPS"; vi init = {0, 1, 2, 2, 1, 1, 0, 2, 0, 0}; int main() { vector<strat*> strats; for (lli a = 0; a < (lli)(3); ++a) strats.emplace_back(new strat_period(vi{a})); for (lli a = 0; a < (lli)(3); ++a) for (lli b = 0; b < (lli)(3); ++b) if (a != b) strats.emplace_back(new strat_period(vi{a, b})); for (lli a = 0; a < (lli)(3); ++a) for (lli b = 0; b < (lli)(3); ++b) for (lli c = 0; c < (lli)(3); ++c) if (a != b || b != c || a != c) strats.emplace_back(new strat_period(vi{a, b, c})); for (lli a = 0; a < (lli)(3); ++a) for (lli b = 0; b < (lli)(3); ++b) strats.emplace_back(new strat_last(a, b)); for (lli i = 0; i < (lli)(10); ++i) { cout << RPS[init[i]] << endl << flush; vector<strat*> nstrats; bool ok; string t; cin >> t; ok = t == "player"; for (auto s : strats) { bool sok = s->update(init[i]); if (sok == ok) nstrats.emplace_back(s); } strats = nstrats; } for (lli i = 0; i < (lli)(10); ++i) { int x = strats[0]->next(); cout << RPS[(x + 1) % 3] << endl; strats[0]->update((x + 1) % 3); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string u, v, s = "RRRRPSSPRR"; for (int i = 0; i < 10; i++) { cout << s[i] << endl; getline(cin, v); if (v == "ai") u += "0"; else u += "1"; } if (u == "1111000011") s = "RRRRRRRRRR"; if (u == "0000100100") s = "PPPPPPPPPP"; if (u == "0000011000") s = "SSSSSSSSSS"; if (u == "0010000010") s = "SRPSRPSRPS"; if (u == "0000110000") s = "PSRPSRPSRP"; if (u == "0000000110") s = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { cout << s[i] << endl; cout.flush(); getline(cin, v); } }
#include <bits/stdc++.h> namespace ringo { template <class T> inline void read(T &x) { x = 0; register char c = getchar(); register bool f = 0; while (!isdigit(c)) f ^= c == '-', c = getchar(); while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); if (f) x = -x; } template <class T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar('0' + x % 10); } template <class T> inline void print(T x, char c) { print(x), putchar(c); } const std::string T = "RRRRPSSPRR"; std::string s, t, input; void main() { for (int i = 0; i < 10; i++) { printf("%c\n", T[i]); getline(std::cin, input); s += (input == "ai" ? '0' : '1'); } if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "1111000011") t = "RRRRRRRRRR"; if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0000000110") t = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { printf("%c\n", t[i]); getline(std::cin, input); } return; } } // namespace ringo signed main() { return ringo::main(), 0; }
#include <bits/stdc++.h> using namespace std; int main() { string a, b; string c = "RRRRPSSPRR"; for (int i = 0; i < 10; i++) { cout << c[i] << endl; cout.flush(); getline(cin, b); if (b == "ai") a += "0"; else a += "1"; } if (a == "1111000011") c = "RRRRRRRRRR"; if (a == "0000100100") c = "PPPPPPPPPP"; if (a == "0000011000") c = "SSSSSSSSSS"; if (a == "0010000010") c = "SRPSRPSRPS"; if (a == "0000110000") c = "PSRPSRPSRP"; if (a == "0000000110") c = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { cout << c[i] << endl; cout.flush(); getline(cin, b); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string a, w, b = "RRRRPSSPRR"; for (int i = 0; i < 10; i++) { cout << b[i] << endl; cout.flush(); getline(cin, w); if (w == "ai") a += "0"; else a += "1"; } if (a == "1111000011") b = "RRRRRRRRRR"; if (a == "0000100100") b = "PPPPPPPPPP"; if (a == "0000011000") b = "SSSSSSSSSS"; if (a == "0010000010") b = "SRPSRPSRPS"; if (a == "0000110000") b = "PSRPSRPSRP"; if (a == "0000000110") b = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { cout << b[i] << endl; cout.flush(); getline(cin, w); } return 0; }
#include <bits/stdc++.h> using namespace std; const string S = "RRRRPSSPRR"; string s, s1, t; int main() { string s, t; for (int i = 0; i < 10; i++) { cout << S[i] << endl; fflush(stdout); getline(cin, s1); s += (s1 == "ai" ? '0' : '1'); } if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "1111000011") t = "RRRRRRRRRR"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "0000000110") t = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { cout << t[i] << endl; fflush(stdout); getline(cin, s1); } }
#include <bits/stdc++.h> using namespace std; int main() { string u, v, s = "RRRRPSSPRR"; for (int i = 0; i < 10; i++) { cout << s[i] << endl; cout.flush(); getline(cin, v); if (v == "ai") u += "0"; else u += "1"; } if (u == "1111000011") s = "RRRRRRRRRR"; if (u == "0000100100") s = "PPPPPPPPPP"; if (u == "0000011000") s = "SSSSSSSSSS"; if (u == "0010000010") s = "SRPSRPSRPS"; if (u == "0000110000") s = "PSRPSRPSRP"; if (u == "0000000110") s = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { cout << s[i] << endl; cout.flush(); getline(cin, v); } }
#include <bits/stdc++.h> using namespace std; int main() { cout << "R" << endl; cout << "R" << endl; cout << "S" << endl; cout << "S" << endl; cout << "P" << endl; cout << "P" << endl; cout << "R" << endl; vector<string> verd(7); for (int i = 0; i < 7; ++i) { cin >> verd[i]; } map<char, char> to; if (verd[1] == "player") { to['R'] = 'R'; } else if (verd[2] == "player") { to['R'] = 'S'; } else { to['R'] = 'P'; } if (verd[3] == "player") { to['S'] = 'S'; } else if (verd[4] == "player") { to['S'] = 'P'; } else { to['S'] = 'R'; } if (verd[5] == "player") { to['P'] = 'P'; } else if (verd[6] == "player") { to['P'] = 'R'; } else { to['P'] = 'S'; } char cur = 'P'; for (int i = 0; i < 13; ++i) { cur = to[cur]; cout << cur << endl; } return 0; }
#include <bits/stdc++.h> const char* T = "RRRRPSSPRR"; using namespace std; string s, s1; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); for (int t = 0; t < 10; t++) { cout << T[t] << endl; getline(cin, s1); s += (s1 == "ai") ? '0' : '1'; } string ans; if (s == "0000100100") ans = "PPPPPPPPPP"; if (s == "0000011000") ans = "SSSSSSSSSS"; if (s == "1111000011") ans = "RRRRRRRRRR"; if (s == "0010000010") ans = "SRPSRPSRPS"; if (s == "0000110000") ans = "PSRPSRPSRP"; if (s == "0000000110") ans = "SPRSPRSPRS"; for (int t = 0; t < 10; t++) { cout << ans[t] << endl; getline(cin, s1); } return 0; }
#include <bits/stdc++.h> using namespace std; const string T = "RRRRPSSPRR"; string s, t, s1; int main() { string s, t; for (int i = 0; i < 10; i++) { printf("%c\n", T[i]); fflush(stdout); getline(cin, s1); s += (s1 == "ai" ? '0' : '1'); } if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "1111000011") t = "RRRRRRRRRR"; if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0000000110") t = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { printf("%c\n", t[i]); fflush(stdout); getline(cin, s1); } return 0; }
#include <bits/stdc++.h> using namespace std; string g = "RRRRPSSPRR", st, s, t; int main() { for (int i = 0; i < 10; i++) cout << g[i] << endl, getline(cin, st), s += st == "ai" ? '0' : '1'; if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "1111000011") t = "RRRRRRRRRR"; if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0000000110") t = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) cout << t[i] << endl, getline(cin, st); return 0; }
#include <bits/stdc++.h> template <typename T> T read() { T res = 0, flag = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') flag = -1; ch = getchar(); } while (isdigit(ch)) res = res * 10 + ch - 48, ch = getchar(); return res * flag; } template <typename T> void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); } namespace A { int a[20]; void solution() { int n = read<int>(), ans = 0; for (int i = (1); i <= (n); ++i) a[i] = read<int>(); for (int i = 1; i <= n; i <<= 1) for (int j = 1; j <= n; j += i) { bool flag = true; for (int k = j + 1; k <= j + i - 1; k++) if (a[k - 1] > a[k]) flag = false; if (flag) ans = std::max(ans, i); } print(ans), putchar('\n'); } } // namespace A namespace B { int arr[] = {2, 3, 4, 5, 12, 30, 35, 43, 46, 52, 64, 86}; void solution() { int n = read<int>(); for (int i = (0); i <= (11); ++i) if (arr[i] == n) { puts("YES"); return; } puts("NO"); } } // namespace B namespace C { int arr[]{15, 14, 12, 13, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7}; void solution() { print(arr[read<int>()]), putchar('\n'); } } // namespace C namespace D { int a[20]; void solution() { int n = read<int>(), min = 1e9; for (int i = (1); i <= (n); ++i) a[i] = read<int>(), min = std::min(min, a[i]); print((min ^ a[3]) + 2), putchar('\n'); } } // namespace D namespace E { void solution() { puts( "0\n1\n1\n0\n1\n1\n0\n1\n1\n1\n1\n1\n0\n1\n0\n1\n1\n1\n0\n1\n1\n1\n1\n1\n" "0\n1\n0\n1\n1\n1"); } } // namespace E namespace F { void solution() { int a, b; a = b = 0; std::string s; std::cin >> s; std::set<char> set = {'A', 'E', 'F', 'H', 'I', 'K', 'L', 'M', 'N', 'T', 'V', 'W', 'X', 'Y', 'Z'}; for (auto x : s) { if ((x >= 'A') && (x <= 'Z')) { if (set.count(x)) a += 1; else b += 1; } } puts(((a == 0) || (b == 0)) ? "YES" : "NO"); } } // namespace F namespace G { void solution() { std::string s, t, s1, T = "RRRRPSSPRR"; for (int i = 0; i < 10; i++) { putchar(T[i]), putchar('\n'); fflush(stdout); std::getline(std::cin, s1); s += (s1 == "ai" ? '0' : '1'); } if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "1111000011") t = "RRRRRRRRRR"; if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0000000110") t = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { putchar(t[i]), putchar('\n'); fflush(stdout); std::getline(std::cin, s1); } } } // namespace G signed main() { 42; G::solution(); }
#include <bits/stdc++.h> using namespace std; int main() { string u, v, s = "RRRRPSSPRR"; for (int i = 0; i < 10; i++) { cout << s[i] << endl; cout.flush(); getline(cin, v); if (v == "ai") u += "0"; else u += "1"; } if (u == "1111000011") s = "RRRRRRRRRR"; if (u == "0000100100") s = "PPPPPPPPPP"; if (u == "0000011000") s = "SSSSSSSSSS"; if (u == "0010000010") s = "SRPSRPSRPS"; if (u == "0000110000") s = "PSRPSRPSRP"; if (u == "0000000110") s = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { cout << s[i] << endl; cout.flush(); getline(cin, v); } }
#include <bits/stdc++.h> using namespace std; int main() { string a, w, b = "RRRRPSSPRR"; for (int i = 0; i < 10; i++) { cout << b[i] << "\n"; getline(cin, w); if (w == "ai") a += "0"; else a += "1"; } if (a == "1111000011") b = "RRRRRRRRRR"; if (a == "0000100100") b = "PPPPPPPPPP"; if (a == "0000011000") b = "SSSSSSSSSS"; if (a == "0010000010") b = "SRPSRPSRPS"; if (a == "0000110000") b = "PSRPSRPSRP"; if (a == "0000000110") b = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { cout << b[i] << endl; getline(cin, w); } return 0; }
#include <bits/stdc++.h> using namespace std; const string T = "RRRRPSSPRR"; string s, t, st; int main() { for (register int i = 0; i < 10; ++i) { printf("%c\n", T[i]); getline(cin, st); s += (st == "ai" ? '0' : '1'); } if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000000110") t = "SPRSPRSPRS"; if (s == "1111000011") t = "RRRRRRRRRR"; for (register int i = 0; i < 10; ++i) { printf("%c\n", t[i]); getline(cin, st); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { const string T = "RRRRPSSPRR"; string s, t; for (int i = 0; i < 10; i++) { printf("%c\n", T[i]); fflush(stdout); string verdict; getline(cin, verdict); s += (verdict == "ai" ? '0' : '1'); } if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "1111000011") t = "RRRRRRRRRR"; if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0000000110") t = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { printf("%c\n", t[i]); fflush(stdout); string verdict; getline(cin, verdict); } return 0; }
#include <bits/stdc++.h> using namespace std; string s = "RRRRPSSPRR"; int main() { string u, v; for (int i = 0; i < 10; i++) { cout << s[i] << '\n'; cout.flush(); getline(cin, v); u += ((v == "ai") ? "0" : "1"); } if (u == "1111000011") s = "RRRRRRRRRR"; if (u == "0000100100") s = "PPPPPPPPPP"; if (u == "0000011000") s = "SSSSSSSSSS"; if (u == "0010000010") s = "SRPSRPSRPS"; if (u == "0000110000") s = "PSRPSRPSRP"; if (u == "0000000110") s = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { cout << s[i] << '\n'; cout.flush(); getline(cin, v); } return 0; }
#include <bits/stdc++.h> using namespace std; void paperStrategy() { for (int i = 0; i < 10; i++) { cout << "S" << endl; cout.flush(); string verdict; getline(cin, verdict); } } void scissorsStrategy() { for (int i = 0; i < 10; i++) { cout << "R" << endl; cout.flush(); string verdict; getline(cin, verdict); } } void rockStrategy() { for (int i = 0; i < 10; i++) { cout << "P" << endl; cout.flush(); string verdict; getline(cin, verdict); } } void RPStrategy() { for (int i = 0; i < 10; i++) { if (i % 3 == 0) cout << "S" << endl; else if (i % 3 == 1) cout << "R" << endl; else cout << "P" << endl; cout.flush(); string verdict; getline(cin, verdict); } } void lastMoveStrategy() { for (int i = 0; i < 10; i++) { if (i % 3 == 0) cout << "R" << endl; else if (i % 3 == 1) cout << "P" << endl; else cout << "S" << endl; cout.flush(); string verdict; getline(cin, verdict); } } void beatingLastMoveStrategy() { for (int i = 0; i < 10; i++) { if (i % 3 == 0) cout << "P" << endl; else if (i % 3 == 1) cout << "R" << endl; else cout << "S" << endl; cout.flush(); string verdict; getline(cin, verdict); } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); bool aiWon[10]; int wins = 0; for (int i = 0; i < 10; i++) { if (i / 3 == 0) cout << "R" << endl; else if (i / 3 == 1) cout << "P" << endl; else cout << "S" << endl; cout.flush(); string verdict; getline(cin, verdict); if (verdict == "player") { aiWon[i] = false; wins++; } else { aiWon[i] = true; } } if (wins == 4) paperStrategy(); else if (wins == 3) { if (!aiWon[0]) scissorsStrategy(); else if (!aiWon[2]) RPStrategy(); else rockStrategy(); } else if (wins == 2) lastMoveStrategy(); else beatingLastMoveStrategy(); }
#include <bits/stdc++.h> using namespace std; template <typename T> void maxtt(T& t1, T t2) { t1 = max(t1, t2); } template <typename T> void mintt(T& t1, T t2) { t1 = min(t1, t2); } bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = "URDL"; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "YES" : "NO"); exit(0); } void addmod(int& x, int y, int mod = 998244353) { assert(y >= 0); x += y; if (x >= mod) x -= mod; assert(x >= 0 && x < mod); } void et(int x = -1) { printf("%d\n", x); exit(0); } long long fastPow(long long x, long long y, int mod = 998244353) { long long ans = 1; while (y > 0) { if (y & 1) ans = (x * ans) % mod; x = x * x % mod; y >>= 1; } return ans; } long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } void fmain(int tid) { string a = "RRPPSS"; string s = "", t = ""; for (int(i) = 0; (i) < (int)(6); (i)++) { cout << a[i] << endl; string z; cin >> z; s += z == "ai" ? "1" : "0"; } if (s == "110011") for (int(i) = 0; (i) < (int)(14); (i)++) cout << "P" << endl; if (s == "111100") for (int(i) = 0; (i) < (int)(14); (i)++) cout << "S" << endl; if (s == "001111") for (int(i) = 0; (i) < (int)(14); (i)++) cout << "R" << endl; if (s == "111001" || s == "110101") t = "PSRPSRPSRPSRPSR"; if (s == "111111") t = "PRSPRSPRSPRSPRS"; if (t != "") { for (int(i) = 0; (i) < (int)(14); (i)++) cout << t[i] << endl; } } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string u, v, s = "RRRRPSSPRR"; for (int i = 0; i < 10; i++) { cout << s[i] << endl; cout.flush(); getline(cin, v); if (v == "ai") u += "0"; else u += "1"; } if (u == "1111000011") s = "RRRRRRRRRR"; if (u == "0000100100") s = "PPPPPPPPPP"; if (u == "0000011000") s = "SSSSSSSSSS"; if (u == "0010000010") s = "SRPSRPSRPS"; if (u == "0000110000") s = "PSRPSRPSRP"; if (u == "0000000110") s = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { cout << s[i] << endl; cout.flush(); getline(cin, v); } return 0; }
#include <bits/stdc++.h> using namespace std; const string T = "RRRRPSSPRR"; string s, t, s1; int main() { ios::sync_with_stdio(false); string s, t; for (register int i = 0; i < 10; i++) { cout << T[i] << endl; getline(cin, s1); s += (s1 == "ai" ? '0' : '1'); } if (s == "0000100100") t = "PPPPPPPPPP"; else if (s == "0000011000") t = "SSSSSSSSSS"; else if (s == "1111000011") t = "RRRRRRRRRR"; else if (s == "0010000010") t = "SRPSRPSRPS"; else if (s == "0000110000") t = "PSRPSRPSRP"; else if (s == "0000000110") t = "SPRSPRSPRS"; for (register int i = 0; i < 10; i++) cout << t[i] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; string T = "RRRRPSSPRR", s, t, s1; signed main() { for (int i = 0; i < 10; i++) { cout << T[i] << endl; fflush(stdout); getline(cin, s1); s += (s1 == "ai" ? '0' : '1'); } if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "1111000011") t = "RRRRRRRRRR"; if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0000000110") t = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { cout << t[i] << endl; fflush(stdout); getline(cin, s1); } return 0; }
#include <bits/stdc++.h> using namespace std; const string T = "RRRRPSSPRR"; string s, t, s1; int main() { string s, t; for (register int i = 0; i < 10; i++) { printf("%c\n", T[i]); fflush(stdout); getline(cin, s1); s += (s1 == "ai" ? '0' : '1'); } if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "1111000011") t = "RRRRRRRRRR"; if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0000000110") t = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { printf("%c\n", t[i]); fflush(stdout); getline(cin, s1); } puts(""); return 0; }
#include <bits/stdc++.h> using namespace std; char res[20]; char qr[] = {'R', 'P', 'R', 'R', 'R', 'R', 'P', 'P', 'S', 'S'}; char seq[] = {'R', 'P', 'S'}; int main() { char verdict[20]; for (int i = 0; i < 10; i++) { printf("%c\n", qr[i]); fflush(stdout); scanf("%s", verdict); if (verdict[0] == 'p') res[i] = 1; } if (true) { int ok = true; for (int i = 0; i < 10; i++) { if (qr[i] == 'P' && res[i] != 1) ok = false; if (qr[i] != 'P' && res[i] == 1) ok = false; } if (ok) { for (int i = 0; i < 10; i++) { printf("P\n"); fflush(stdout); scanf("%s", verdict); } return 0; } } if (true) { int ok = true; for (int i = 0; i < 10; i++) { if (qr[i] == 'S' && res[i] != 1) ok = false; if (qr[i] != 'S' && res[i] == 1) ok = false; } if (ok) { for (int i = 0; i < 10; i++) { printf("S\n"); fflush(stdout); scanf("%s", verdict); } return 0; } } if (true) { int ok = true; for (int i = 0; i < 10; i++) { if (qr[i] == 'R' && res[i] != 1) ok = false; if (qr[i] != 'R' && res[i] == 1) ok = false; } if (ok) { for (int i = 0; i < 10; i++) { printf("R\n"); fflush(stdout); scanf("%s", verdict); } return 0; } } if (true) { int ok = true; for (int i = 0; i < 10; i++) { if (qr[i] == seq[(i + 1) % 3] && res[i] != 1) ok = false; if (qr[i] != seq[(i + 1) % 3] && res[i] == 1) ok = false; } if (ok) { for (int i = 0; i < 10; i++) { printf("%c\n", seq[(2 + i) % 3]); fflush(stdout); scanf("%s", verdict); } return 0; } } if (true) { int ok = true; for (int i = 0; i < 10; i++) { if (qr[i] == seq[(i + 2) % 3] && res[i] != 1) ok = false; if (qr[i] != seq[(i + 2) % 3] && res[i] == 1) ok = false; } if (ok) { for (int i = 0; i < 10; i++) { printf("%c\n", seq[i % 3]); fflush(stdout); scanf("%s", verdict); } return 0; } } if (true) { int ok = true; for (int i = 0; i < 10; i++) { if (qr[i] == seq[i % 3] && res[i] != 1) ok = false; if (qr[i] != seq[i % 3] && res[i] == 1) ok = false; } if (ok) { for (int i = 0; i < 10; i++) { printf("%c\n", seq[(1 + i) % 3]); fflush(stdout); scanf("%s", verdict); } return 0; } } if (true) { int ok = true; for (int i = 0; i < 10; i++) { if (i % 2 == 0) { if (qr[i] == 'R' && res[i] != 1) ok = false; if (qr[i] != 'R' && res[i] == 1) ok = false; } else { if (qr[i] == 'P' && res[i] != 1) ok = false; if (qr[i] != 'P' && res[i] == 1) ok = false; } } if (ok) { for (int i = 0; i < 10; i++) { if (i % 2 == 0) printf("R\n"); else printf("P\n"); fflush(stdout); scanf("%s", verdict); } return 0; } } if (true) { int ok = true; for (int i = 0; i < 10; i++) { if (i % 2 == 0) { if (qr[i] == 'S' && res[i] != 1) ok = false; if (qr[i] != 'S' && res[i] == 1) ok = false; } else { if (qr[i] == 'P' && res[i] != 1) ok = false; if (qr[i] != 'P' && res[i] == 1) ok = false; } } if (ok) { for (int i = 0; i < 10; i++) { if (i % 2 == 0) printf("S\n"); else printf("P\n"); fflush(stdout); scanf("%s", verdict); } return 0; } } if (res[2] && res[5] && res[6]) return 0; if (true) { if (res[1]) { for (int i = 0; i < 10; i++) { printf("%c\n", seq[i % 3]); fflush(stdout); scanf("%s", verdict); } return 0; } } if (true) { int ok = true; for (int i = 3; i < 10; i++) if (res[i]) ok = false; if (ok && res[2]) { int cur = 1; for (int i = 0; i < 10; i++) { printf("%c\n", seq[cur % 3]); fflush(stdout); scanf("%s", verdict); cur += 2; } return 0; } } if (true) { if (res[3]) { for (int i = 0; i < 10; i++) { printf("S\n"); fflush(stdout); scanf("%s", verdict); } return 0; } } while (1) ; return 1; }
#include <bits/stdc++.h> using namespace std; const string T = "RRRRPSSPRR"; string s, t, s1; int main() { string s, t; for (register int i = 0; i < 10; i++) { printf("%c\n", T[i]); fflush(stdout); getline(cin, s1); s += (s1 == "ai" ? '0' : '1'); } if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "1111000011") t = "RRRRRRRRRR"; if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0000000110") t = "SPRSPRSPRS"; for (register int i = 0; i < 10; i++) { printf("%c\n", t[i]); fflush(stdout); getline(cin, s1); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { cout << "R" << endl << "R" << endl << "S" << endl << "S" << endl << "P" << endl << "P" << endl << "R" << endl; vector<string> verd(7); for (int i = 0; i < 7; i++) { cin >> verd[i]; } map<char, char> to; if (verd[1] == "player") { to['R'] = 'R'; } else if (verd[2] == "player") { to['R'] = 'S'; } else { to['R'] = 'P'; } if (verd[3] == "player") { to['S'] = 'S'; } else if (verd[4] == "player") { to['S'] = 'P'; } else { to['S'] = 'R'; } if (verd[5] == "player") { to['P'] = 'P'; } else if (verd[6] == "player") { to['P'] = 'R'; } else { to['P'] = 'S'; } char cur = 'P'; for (int i = 0; i < 13; i++) { cur = to[cur]; cout << cur << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string a = "RRPPSS"; string ans[6] = {"PPPPPPPPPP", "SSSSSSSSSS", "RRRRRRRRRR", "PSRPSRPSRP", "RPSRPSRPSR", "PRSPRSPRSP"}; int ver[20]; memset(ver, 0, sizeof(ver)); int mode; for (int i = 0; i < 6; ++i) { cout << a[i] << endl; cout.flush(); string verdict; getline(cin, verdict); if (verdict == "ai") ver[i] = 0; else ver[i] = 1; if (i == 5) { if (ver[0] == 0 and ver[1] == 0 and ver[2] == 1 and ver[3] == 1 and ver[4] == 0 and ver[5] == 0) { mode = 0; } if (ver[0] == 0 and ver[1] == 0 and ver[2] == 0 and ver[3] == 0 and ver[4] == 1 and ver[5] == 1) { mode = 1; } if (ver[0] == 1 and ver[1] == 1 and ver[2] == 0 and ver[3] == 0 and ver[4] == 0 and ver[5] == 0) { mode = 2; } if (ver[0] == 0 and ver[1] == 0 and ver[2] == 0 and ver[3] == 1 and ver[4] == 1 and ver[5] == 0) { mode = 3; } if (ver[0] == 0 and ver[1] == 0 and ver[2] == 1 and ver[3] == 0 and ver[4] == 1 and ver[5] == 0) { mode = 4; } if (ver[0] == 0 and ver[1] == 0 and ver[2] == 0 and ver[3] == 0 and ver[4] == 0 and ver[5] == 0) { mode = 5; } } } for (int i = 0; i < 10; ++i) { cout << ans[mode][i] << endl; cout.flush(); string verdict; getline(cin, verdict); } return 0; }
#include <bits/stdc++.h> #pragma GCC optimaze("Ofast") using namespace std; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << "R\nR\nS\nS\nP\nP\nR" << endl; vector<string> S(7); for (int i = 0; i < 7; ++i) { cin >> S[i]; } map<char, char> A; if (S[1] == "player") { A['R'] = 'R'; } else if (S[2] == "player") { A['R'] = 'S'; } else { A['R'] = 'P'; } if (S[3] == "player") { A['S'] = 'S'; } else if (S[4] == "player") { A['S'] = 'P'; } else { A['S'] = 'R'; } if (S[5] == "player") { A['P'] = 'P'; } else if (S[6] == "player") { A['P'] = 'R'; } else { A['P'] = 'S'; } char last = 'P'; for (int i = 0; i < 13; ++i) { last = A[last]; cout << last << endl; } }
#include <bits/stdc++.h> using namespace std; string s, t, s1, T = "RRRRPSSPRR"; int main() { for (int i = 0; i < 10; i++) { printf("%c\n", T[i]); fflush(stdout); getline(cin, s1); s += (s1 == "ai" ? '0' : '1'); } if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "1111000011") t = "RRRRRRRRRR"; if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0000000110") t = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { printf("%c\n", t[i]); fflush(stdout); getline(cin, s1); } return 0; }
#include <bits/stdc++.h> using namespace std; const string T = "RRRRPSSPRR"; string s, t, s1; int main() { ios::sync_with_stdio(false); for (register int i = 0; i < T.length(); i++) { cout << T[i] << endl; getline(cin, s1); if (s1 == "ai") s += '0'; else s += '1'; } if (s == "0000100100") t = "PPPPPPPPPP"; else if (s == "0000011000") t = "SSSSSSSSSS"; else if (s == "1111000011") t = "RRRRRRRRRR"; else if (s == "0010000010") t = "SRPSRPSRPS"; else if (s == "0000110000") t = "PSRPSRPSRP"; else if (s == "0000000110") t = "SPRSPRSPRS"; for (register int i = 0; i < t.length(); i++) cout << t[i] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s, t, s1, T = "RRRRPSSPRR"; int i; for (i = 0; i < 10; i++) { cout << T[i] << endl; fflush(stdout); getline(cin, s1); s += (s1 == "ai" ? '0' : '1'); } if (s == "0000100100") { t = "PPPPPPPPPP"; } if (s == "0000011000") { t = "SSSSSSSSSS"; } if (s == "1111000011") { t = "RRRRRRRRRR"; } if (s == "0010000010") { t = "SRPSRPSRPS"; } if (s == "0000110000") { t = "PSRPSRPSRP"; } if (s == "0000000110") { t = "SPRSPRSPRS"; } for (i = 0; i < 10; i++) { cout << t[i] << endl; fflush(stdout); getline(cin, s1); } return 0; }
#include <bits/stdc++.h> using namespace std; const string T = "RRRRPSSPRR"; string s, t, s1; int main() { string s, t; for (int i = 0; i < 10; i++) { printf("%c\n", T[i]); fflush(stdout); getline(cin, s1); s += (s1 == "ai" ? '0' : '1'); } if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "1111000011") t = "RRRRRRRRRR"; if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0000000110") t = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { printf("%c\n", t[i]); fflush(stdout); getline(cin, s1); } return 0; }
#include <bits/stdc++.h> using namespace std; const string T = "RRRRPSSPRR"; string s, t, s1; int main() { string s, t; for (int i = 0; i < 10; i++) { printf("%c\n", T[i]); fflush(stdout); getline(cin, s1); s += (s1 == "ai" ? '0' : '1'); } if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "1111000011") t = "RRRRRRRRRR"; if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0000000110") t = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { cout << t[i] << endl; fflush(stdout); getline(cin, s1); } return 0; }
#include <bits/stdc++.h> using namespace std; #pragma GCC target("avx2") #pragma GCC optimization("O2") #pragma GCC optimization("unroll-loops") const long double ERR = 1e-5; const int MOD = 1e9 + 7; bool areEqual(long double _n1, long double _n2) { return fabs(_n1 - _n2) < ERR; } string OUTCOME = ""; string PLAYERStart = "SRPSRPSRPP"; vector<string> PLAYER = {"PPPPPPPPPP", "SSSSSSSSSS", "RRRRRRRRRR", "SRPSRPSRPS", "SRPSRPSRPS", "RSPRSPRSPR"}; vector<string> AI = {"RRRRRRRRRR RRRRRRRRRR", "PPPPPPPPPP PPPPPPPPPP", "SSSSSSSSSS SSSSSSSSSS", "RPSRPSRPSR PSRPSRPSRP", "RSRPSRPSRP PSRPSRPSRP", "PRPSRPSRPS RSPRSPRSPR"}; void testOutStrategy(); void playStrategy(string); int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); testOutStrategy(); if (OUTCOME == "LLWLLWLLWW") playStrategy(PLAYER[0]); else if (OUTCOME == "WLLWLLWLLL") playStrategy(PLAYER[1]); else if (OUTCOME == "LWLLWLLWLL") playStrategy(PLAYER[2]); else if (OUTCOME == "LLLLLLLLLW") playStrategy(PLAYER[3]); else if (OUTCOME == "LWWWWWWWWL") playStrategy(PLAYER[4]); else if (OUTCOME == "WLLLLLLLLL") playStrategy(PLAYER[5]); else cout << OUTCOME << '\n'; } void playStrategy(string S) { for (int i = 0; i < S.length(); i++) { cout << S[i] << '\n'; cout.flush(); string verdict; getline(cin, verdict); } } void testOutStrategy() { for (int i = 0; i < 10; i++) { cout << PLAYERStart[i] << '\n'; cout.flush(); string verdict; getline(cin, verdict); OUTCOME += (verdict == "player" ? 'W' : 'L'); } }
#include <bits/stdc++.h> using namespace std; const string T = "RRRRPSSPRR"; string s, t, s1; int main() { string s, t; for (int i = 0; i < 10; i++) { printf("%c\n", T[i]); fflush(stdout); getline(cin, s1); s += (s1 == "ai" ? '0' : '1'); } if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "1111000011") t = "RRRRRRRRRR"; if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0000000110") t = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { printf("%c\n", t[i]); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { const string T = "RRRRPSSPRR"; string s, t; for (int i = 0; i < 10; i++) { printf("%c\n", T[i]); fflush(stdout); string verdict; getline(cin, verdict); s += (verdict == "ai" ? '0' : '1'); } if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "1111000011") t = "RRRRRRRRRR"; if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0000000110") t = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { printf("%c\n", t[i]); fflush(stdout); string verdict; getline(cin, verdict); } return 0; }
#include <bits/stdc++.h> using std::cin; using std::string; const string T = "RRRRPSSPRR"; string s, t, s1; int main() { string s, t; for (int i = 0; i < 10; i++) { printf("%c\n", T[i]); fflush(stdout); getline(cin, s1); s += (s1 == "ai" ? '0' : '1'); } if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "1111000011") t = "RRRRRRRRRR"; if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0000000110") t = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { printf("%c\n", t[i]); fflush(stdout); getline(cin, s1); } return 0; }
#include <bits/stdc++.h> using namespace std; const string T = "RRRRPSSPRR"; string s, t, s1; int main() { string s, t; for (int i = 0; i < 10; i++) { printf("%c\n", T[i]); fflush(stdout); getline(cin, s1); s += (s1 == "ai" ? '0' : '1'); } if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "1111000011") t = "RRRRRRRRRR"; if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0000000110") t = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { printf("%c\n", t[i]); fflush(stdout); getline(cin, s1); } return 0; }
#include <bits/stdc++.h> using namespace std; const string T = "RRRRPSSPRR"; string s, t, s1; int main() { string s, t; for (int i = 0; i < 10; i++) { printf("%c\n", T[i]); getline(cin, s1); s += (s1 == "ai" ? '0' : '1'); } if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "1111000011") t = "RRRRRRRRRR"; if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0000000110") t = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { printf("%c\n", t[i]); getline(cin, s1); } return 0; }
#include <bits/stdc++.h> using namespace std; const string T = "RRRRPSSPRR"; string s, t, s1; int main() { string s, t; for (int i = 0; i < 10; i++) { printf("%c\n", T[i]); fflush(stdout); getline(cin, s1); s += (s1 == "ai" ? '0' : '1'); } if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "1111000011") t = "RRRRRRRRRR"; if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0000000110") t = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { printf("%c\n", t[i]); fflush(stdout); getline(cin, s1); } return 0; }
#include <bits/stdc++.h> using namespace std; const string T = "RRRRPSSPRR"; string s, t, s1; int main() { string s, t; for (int i = 0; i < 10; i++) { printf("%c\n", T[i]); fflush(stdout); getline(cin, s1); s += (s1 == "ai" ? '0' : '1'); } if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "1111000011") t = "RRRRRRRRRR"; if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0000000110") t = "SPRSPRSPRS"; for (int i = 0; i < 10; i++) { printf("%c\n", t[i]); fflush(stdout); getline(cin, s1); } return 0; }
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, k = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') k = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return x * k; } const string emm = "RRRRPSSPRR"; string s, t, s1; int main() { string s, t; for (register int i = 0; i < 10; ++i) { printf("%c\n", emm[i]); getline(cin, s1); s += s1 == "ai" ? '0' : '1'; } if (s == "0000100100") t = "PPPPPPPPPP"; if (s == "0000011000") t = "SSSSSSSSSS"; if (s == "1111000011") t = "RRRRRRRRRR"; if (s == "0010000010") t = "SRPSRPSRPS"; if (s == "0000110000") t = "PSRPSRPSRP"; if (s == "0000000110") t = "SPRSPRSPRS"; for (register int i = 0; i < 10; i++) { printf("%c\n", t[i]); getline(cin, s1); } return 0; }
#include <bits/stdc++.h> using namespace std; string a, s, t = "RRPPSS"; int main() { ios_base::sync_with_stdio(false); for (int i = 0; i < 6; ++i) { cout << t[i] << endl; cin >> a; s += a[0]; } if (s == "aappaa") t = "PPP"; if (s == "aaaapp") t = "SSS"; if (s == "ppaaaa") t = "RRR"; if (s == "aaappa") t = "PSR"; if (s == "aapapa") t = "RPS"; if (s == "aaaaaa") t = "PRS"; for (int i = 0; i < 10; ++i) { cout << t[i % 3] << endl; cin >> a; } return 0; }
#include <bits/stdc++.h> using namespace std; map<long long, long long> mp[100001]; long long n, m, q, u, v, c, f[100001]; set<long long> a[100001]; char op; inline long long read() { long long sum = 0, x = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') x = -1; ch = getchar(); } while (isdigit(ch)) { sum = (sum << 1) + (sum << 3) + (ch ^ '0'); ch = getchar(); } return sum * x; } inline void write(long long x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + '0'); } inline long long find(long long x) { if (x == f[x]) return x; return f[x] = find(f[x]); } inline void jh(long long x, long long y) { long long u = find(x), v = find(y); if (u == v) return; if (a[u].size() < a[v].size()) swap(u, v); f[v] = u; for (long long i : a[v]) a[u].insert(i); a[v].clear(); } inline void add(long long u, long long v, long long c) { a[find(u)].insert(v); if (mp[u].count(c)) { long long x = mp[u][c]; jh(v, x); } else mp[u][c] = v; } signed main() { n = read(); m = read(); read(); q = read(); for (register long long i = 1; i <= n; ++i) f[i] = i; while (m--) { u = read(); v = read(); c = read(); add(u, v, c); add(v, u, c); } while (q--) { cin >> op; u = read(); v = read(); if (op == '+') { c = read(); add(u, v, c); add(v, u, c); } else { long long x = find(u), y = find(v); if (x == y || a[x].count(v)) puts("Yes"); else puts("No"); } } return 0; }
#include <bits/stdc++.h> using namespace std; char _; char ch; bool is_big[200006]; int n, m, c, q, first, second, z, r2[100005]; vector<int> com2[100005]; map<int, int> r[100005]; map<int, vector<int> > com[100005]; set<int> near[100005]; map<int, int> odd[100005], par[100005], nxt[100005]; vector<int> v[200005]; vector<int> big; void mrg2(int A, int B) { A = r2[A]; B = r2[B]; if (A == B) return; if (com2[A].size() < com2[B].size()) { mrg2(B, A); return; } for (int l = 0; l < big.size(); ++l) { if (near[big[l]].count(B)) { near[big[l]].insert(A); } } for (auto i : com2[B]) { com2[A].push_back(i); r2[i] = A; } } void init(int A, int C) { if (r[A].count(C) == 0) { r[A][C] = A; com[A][C] = vector<int>({A}); par[A][C] = 1; odd[A][C] = 0; } } void clean(int A, int C) { for (auto i : com[A][C]) { mrg2(i, A); } odd[A][C] = 1; } void make_big(int OB) { if (v[OB].size() == 400) { for (int l = 0; l < v[OB].size(); ++l) { near[OB].insert(r2[v[OB][l]]); } big.push_back(OB); is_big[OB] = 1; } } void mrg(int A, int B, int C, int OA, int OB) { init(A, C); init(B, C); int RA = r[A][C], RB = r[B][C]; if (RA == RB && par[A][C] == par[B][C] || odd[RA][C] || odd[RB][C]) { if (!odd[RA][C]) { clean(RA, C); } if (!odd[RB][C]) { clean(RB, C); } } A = RA; B = RB; if (A == B) { return; } if (com[A][C].size() < com[B][C].size()) { mrg(B, A, C, OB, OA); return; } vector<int> tmp = com[B][C]; int dir = par[OB][C]; for (auto i : tmp) { r[i][C] = A; com[A][C].push_back(i); if (par[i][C] ^ dir ^ 1) { if (nxt[OA].count(C)) mrg2(nxt[OA][C], i); } else { mrg2(OA, i); } par[i][C] = (par[OA][C] ^ par[i][C] ^ dir ^ 1) % 2; } nxt[OA][C] = OB; nxt[OB][C] = OA; v[OA].push_back(OB); v[OB].push_back(OA); if (is_big[OA]) { near[OA].insert(OB); } if (is_big[OB]) { near[OB].insert(OA); } make_big(OA); make_big(OB); } int32_t main() { scanf("%i%i%i%i", &n, &m, &c, &q); for (int l = 1; l <= n; ++l) { com2[l] = vector<int>({l}); r2[l] = l; } for (int l = 0; l < m; ++l) { scanf("%i%i%i", &first, &second, &z); mrg(first, second, z, first, second); } for (int l = 0; l < q; ++l) { scanf(" %c%i%i", &ch, &first, &second); if (ch == '+') { scanf("%i", &z); mrg(first, second, z, first, second); } else { bool done = 0; if (v[second].size() < 400) { for (int l2 = 0; l2 < v[second].size(); ++l2) { if (r2[first] == r2[v[second][l2]]) { printf("Yes\n"); done = 1; break; } } } else { if (near[second].count(r2[first])) { printf("Yes\n"); done = 1; } } if (done) continue; if (r2[first] == r2[second]) { printf("Yes\n"); } else { printf("No\n"); } } } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; const int M = 2e5 + 7; map<int, int> mp[N]; set<int> e[N]; int n, m, c, q, to[N], f[N], siz[N]; int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); } void merge(int x, int y) { x = find(x), y = find(y); if (x == y) return; if (e[x].size() < e[y].size()) swap(x, y); for (int i : e[y]) e[x].insert(i); f[y] = x; } void add_edge(int u, int v, int col) { e[find(u)].insert(v); if (mp[u][col]) merge(mp[u][col], v); else mp[u][col] = v; } void add(int x, int y, int col) { add_edge(x, y, col), add_edge(y, x, col); } int main() { scanf("%d%d%d%d", &n, &m, &c, &q); for (int i = 1; i <= n; i++) f[i] = i; for (int i = 1; i <= m; i++) { int u, v, col; scanf("%d%d%d", &u, &v, &col); add(u, v, col); } while (q--) { char s[9]; int u, v, col; scanf("%s%d%d", s, &u, &v); if (s[0] == '?') printf("%s\n", find(u) == find(v) ? "Yes" : (*e[find(u)].lower_bound(v) == v ? "Yes" : "No")); else scanf("%d", &col), add(u, v, col); } return 0; }
#include <bits/stdc++.h> using namespace std; const int nax = 1e6; set<int> nbs[nax]; int par[nax]; int find(int p) { return par[p] = (par[p] == p ? p : find(par[p])); } void join(int a, int b) { a = find(a), b = find(b); if (a == b) return; if (nbs[b].size() > nbs[a].size()) swap(a, b); par[b] = a; for (int i : nbs[b]) nbs[a].insert(i); nbs[b].clear(); } map<pair<int, int>, int> m; int id(int p, int col) { pair<int, int> pi(p, col); if (!m.count(pi)) { int i = m.size(); m[pi] = i; par[i] = i; nbs[i].insert(p); } return m[pi]; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, cols, q; cin >> n >> m >> cols >> q; while (m--) { int a, b, c; cin >> a >> b >> c; join(id(a, 0), id(b, c)); join(id(a, c), id(b, 0)); } while (q--) { char cmd; cin >> cmd; if (cmd == '+') { int a, b, c; cin >> a >> b >> c; join(id(a, 0), id(b, c)); join(id(a, c), id(b, 0)); } else if (cmd == '?') { int a, b; cin >> a >> b; cout << (nbs[find(id(a, 0))].count(b) ? "Yes" : "No") << '\n'; } else assert(0); } }
#include <bits/stdc++.h> using namespace std; int n, m, c, q; int par[100005] = {0}; map<int, int> color[100005]; set<int> s[100005]; int find(int a) { if (par[a] == a) return a; return par[a] = find(par[a]); } void join(int a, int b) { int repa = find(a); int repb = find(b); if (repa == repb) return; if (s[repa].size() > s[repb].size()) { par[repb] = repa; for (int i : s[repb]) { s[repa].insert(i); } } else { par[repa] = repb; for (int i : s[repa]) { s[repb].insert(i); } } return; } void upd(int a, int b, int c) { if (color[a].count(c)) join(b, color[a][c]); else color[a][c] = b; if (color[b].count(c)) join(a, color[b][c]); else color[b][c] = a; if (find(a) != find(b)) { s[find(a)].insert(b); s[find(b)].insert(a); } return; } bool query(int from, int to) { if (s[find(from)].count(to)) return true; else return false; } void solve() { cin >> n >> m >> c >> q; for (int i = 1; i <= n; i++) { par[i] = i; s[i].insert(i); } for (int i = 1; i <= m; i++) { int a, b, c; cin >> a >> b >> c; upd(a, b, c); } for (int i = 1; i <= q; i++) { char type; cin >> type; if (type == '+') { int a, b, c; cin >> a >> b >> c; upd(a, b, c); } else { int a, b; cin >> a >> b; if (query(a, b)) cout << "Yes" << endl; else cout << "No" << endl; } } return; } int main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const long long int N = 100005; vector<pair<long long int, long long int> > adj[N]; set<pair<long long int, long long int> > ver[N]; set<long long int> odd[N]; long long int par[N]; long long int n, m, q, c; void init() { for (long long int i = 1; i <= n; i++) { par[i] = i; for (auto j : adj[i]) odd[i].insert(j.first); } } long long int find(long long int v) { if (v == par[v]) return v; return par[v] = find(par[v]); } void merge(long long int a, long long int b) { a = find(a); b = find(b); if (a != b) { if (odd[a].size() < odd[b].size()) swap(a, b); for (auto i : odd[b]) odd[a].insert(i); odd[b].clear(); par[b] = a; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; cin >> n >> m >> c >> q; for (long long int i = 0; i < m; i++) { long long int x, y, z; cin >> x >> y >> z; adj[x].push_back({y, z}); adj[y].push_back({x, z}); } init(); for (long long int i = 1; i <= n; i++) { for (auto j : adj[i]) ver[i].insert({j.second, j.first}); set<pair<long long int, long long int> >::iterator it1, it2; for (it1 = ver[i].begin(); it1 != ver[i].end();) { it2 = it1; it2++; pair<long long int, long long int> p = *it1; while (it2 != ver[i].end()) { pair<long long int, long long int> q = *it2; if (p.first != q.first) break; merge(p.second, q.second); it2++; } it1 = it2; } } while (q--) { char ch; cin >> ch; if (ch == '+') { long long int x, y, z; cin >> x >> y >> z; pair<long long int, long long int> p; if (ver[x].upper_bound({z, -1}) != ver[x].end()) { p = *(ver[x].upper_bound({z, -1})); if (p.first == z) merge(y, p.second); } if (ver[y].upper_bound({z, -1}) != ver[y].end()) { p = *(ver[y].upper_bound({z, -1})); if (p.first == z) merge(x, p.second); } ver[x].insert({z, y}); ver[y].insert({z, x}); long long int a = find(x); long long int b = find(y); odd[a].insert(y); odd[b].insert(x); } else { long long int x, y; cin >> x >> y; long long int a = find(x), b = find(y); if (a == b || odd[a].find(y) != odd[a].end()) cout << "Yes\n"; else cout << "No\n"; } } }
#include <bits/stdc++.h> template <typename T1, typename T2> std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template <typename C> std::ostream& _debug_go(std::ostream& os, const C& v) { bool first = true; os << "["; for (auto i : v) { if (!first) os << ", "; os << i; first = false; } return os << "]"; } template <typename T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) { return _debug_go(os, v); } template <typename T> std::ostream& operator<<(std::ostream& os, const std::set<T>& v) { return _debug_go(os, v); } template <typename T1, typename T2> std::ostream& operator<<(std::ostream& os, const std::map<T1, T2>& v) { return _debug_go(os, v); } struct Debugger { template <typename T> Debugger& operator,(const T& v) { std::cerr << v << " "; return *this; } } dbg; using namespace std; using uint = uint32_t; using llint = int64_t; using ullint = uint64_t; template <typename T = int> using Pt = complex<T>; template <typename T> T cross(const Pt<T>& a, const Pt<T>& b) { return a.real() * b.imag() - a.imag() * b.real(); } struct UnionFind { const int N; vector<int> p, size; vector<vector<int>> comps; UnionFind(int N) : N(N), p(N), size(N, 1), comps(N) { iota(p.begin(), p.end(), 0); for (int u = 0; u < N; ++u) { comps[u] = {u}; } } int find(int u) { if (p[u] == u) return u; return p[u] = find(p[u]); } int unite(int u, int v) { u = find(u), v = find(v); if (u == v) return u; if (size[u] < size[v]) swap(u, v); p[v] = u; size[u] += size[v]; for (int vv : comps[v]) { comps[u].push_back(vv); } comps[v].clear(); assert(size[u] == comps[u].size()); return u; } }; struct Graph { const int N, C; vector<vector<pair<int, int>>> E; UnionFind strictDoubleRainbow; vector<map<int, int>> nbr; vector<set<int>> nbrComps; Graph(int N, int C) : N(N), C(C), E(N), strictDoubleRainbow(N), nbr(N), nbrComps(N) {} void addEdge(int x, int y, int z) { set<tuple<int, int, int>> done; for (const auto [x, y] : vector<pair<int, int>>{{x, y}, {y, x}}) { auto [it, inserted] = nbr[x].insert({z, y}); if (!inserted) { int a = strictDoubleRainbow.find(it->second); int b = strictDoubleRainbow.find(y); if (a == b) continue; if (strictDoubleRainbow.size[a] < strictDoubleRainbow.size[b]) { swap(a, b); } for (int v : strictDoubleRainbow.comps[b]) { for (auto [u, _] : E[v]) { bool ok = nbrComps[u].erase(b) || done.find({u, a, b}) != done.end(); assert(ok); nbrComps[u].insert(a); done.insert({u, a, b}); } } strictDoubleRainbow.unite(a, b); } } E[x].push_back({y, z}); nbrComps[x].insert(strictDoubleRainbow.find(y)); E[y].push_back({x, z}); nbrComps[y].insert(strictDoubleRainbow.find(x)); } bool doubleRainbowConnected(int x, int y) { return strictDoubleRainbow.find(x) == strictDoubleRainbow.find(y) || nbrComps[y].find(strictDoubleRainbow.find(x)) != nbrComps[y].end(); } }; struct GraphSlow { const int N, C; vector<vector<pair<int, int>>> E; GraphSlow(int N, int C) : N(N), C(C), E(N) {} void addEdge(int x, int y, int z) { E[x].push_back({y, z}); E[y].push_back({x, z}); } bool doubleRainbowConnected(int x, int y) { vector<int> vis(N); queue<int> q; q.push(x); vis[x] = true; while (!q.empty()) { int u = q.front(); q.pop(); if (u == y) return true; for (auto [u0, z0] : E[u]) { if (u0 == y) return true; for (auto [u1, z1] : E[u0]) if (z0 == z1) { if (!vis[u1]) { q.push(u1); vis[u1] = true; } } } } return false; } }; const int MAX_N = 1e5, MAX_Q = 2e5, MAX_C = 1e5; struct GraphOp { bool addEdgeOrQuery; int x, y, z; friend ostream& operator<<(ostream& out, const GraphOp& op) { if (op.addEdgeOrQuery) { return out << "(+ " << op.x << " " << op.y << " " << op.z << ")"; } else { return out << "(? " << op.x << " " << op.y << ")"; } } }; struct GraphOps { int N, C; vector<GraphOp> ops; friend ostream& operator<<(ostream& out, const GraphOps& ops) { out << "(" << ops.N << ", " << ops.C << ", \n"; for (const auto& op : ops.ops) { out << op << ",\n"; } return out << ")"; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int N, M, C, Q; cin >> N >> M >> C >> Q; Graph g(N, C); for (int i = 0; i < M; ++i) { int x, y, z; cin >> x >> y >> z; --x, --y, --z; g.addEdge(x, y, z); } for (int q = 0; q < Q; ++q) { char tp; cin >> tp; int x, y; cin >> x >> y; --x, --y; if (tp == '+') { int z; cin >> z; --z; g.addEdge(x, y, z); } else { cout << (g.doubleRainbowConnected(x, y) ? "Yes" : "No") << '\n'; } } return 0; }
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); } return x * f; } int n, m, C, Q; map<int, vector<int> > mp[100020]; struct Union_Set { int f[100020]; void init(int n) { for (int i = 1; i <= n; ++i) f[i] = i; } inline int getf(int x) { return f[x] == x ? x : f[x] = getf(f[x]); } inline int Merge(int x, int y) { int fx = getf(x), fy = getf(y); if (fx == fy) return 0; f[fy] = fx; return 1; } } S; set<int> s[100020]; void Merge(int x, int y) { x = S.getf(x), y = S.getf(y); if (mp[x].size() < mp[y].size()) { swap(x, y); } if (!S.Merge(x, y)) return; for (auto u : s[y]) s[x].insert(u); } void Add(int u, int v, int w) { s[S.getf(u)].insert(v); s[S.getf(v)].insert(u); mp[u][w].push_back(v); mp[v][w].push_back(u); Merge(mp[u][w][0], v); Merge(mp[v][w][0], u); } int main() { n = read(), m = read(), C = read(), Q = read(); S.init(n); for (int i = 1; i <= m; ++i) { int u = read(), v = read(), w = read(); Add(u, v, w); } while (Q--) { char opt; do { opt = getchar(); } while (opt != '?' && opt != '+'); if (opt == '+') { int u = read(), v = read(), w = read(); Add(u, v, w); } else { int u = read(), v = read(); printf(S.getf(u) == S.getf(v) || s[S.getf(u)].count(v) ? "Yes\n" : "No\n"); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int kn = 2e5 + 5, N = 1e5 + 5; const long long mod = 1e9 + 7, mod2 = 1e9 + 9; const long long base = 31, base1 = 37; int n, m, color, q; int par[N], size[N]; set<pair<int, int> > G[N]; set<int> adj[N]; int fin(int i) { return (par[i] == i) ? i : par[i] = fin(par[i]); } void dj(int i, int j) { int pi = fin(i), pj = fin(j); if (pi == pj) return; if (size[pj] > size[pi]) swap(pi, pj); par[pj] = pi; size[pi] += size[pj]; for (int t : adj[pj]) adj[pi].insert(t); } void add(int u, int v, int c) { int pu = fin(u), pv = fin(v); adj[pu].insert(v); adj[pv].insert(u); auto it = G[u].lower_bound({c, 0}); if (it != G[u].end() && it->first == c) { dj(it->second, v); } else { G[u].insert(pair<int, int>(c, v)); } it = G[v].lower_bound({c, 0}); if (it != G[v].end() && it->first == c) { dj(it->second, u); } else { G[v].insert(pair<int, int>(c, u)); } } int main() { scanf("%d%d%d%d", &n, &m, &color, &q); for (int i = 1; i <= n; i++) { par[i] = i; size[i] = 1; } while (m--) { int u, v, c; scanf("%d%d%d", &u, &v, &c); add(u, v, c); } while (q--) { char type; int u, v; cin >> type; scanf("%d%d", &u, &v); if (type == '?') { int pu = fin(u), pv = fin(v); if (pu == pv) puts("Yes"); else { if (adj[pu].find(v) != adj[pu].end()) puts("Yes"); else puts("No"); } } else { int c; scanf("%d", &c); add(u, v, c); } } }
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; set<int> ss[MAXN]; int par[MAXN], n, m, C, Q; vector<int> vec[MAXN]; map<int, int> mp[MAXN]; int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); } void merge(int x, int y) { x = find(x), y = find(y); if (x == y) return; if (vec[x].size() < vec[y].size()) swap(x, y); par[y] = x; vec[x].insert(vec[x].end(), vec[y].begin(), vec[y].end()); for (int i : vec[y]) ss[i].insert(x); vec[y].clear(); } void add(int x, int y, int c) { int &t = mp[x][c]; if (!t) t = y; else merge(y, t); int &p = mp[y][c]; if (!p) p = x; else merge(x, p); ss[x].insert(find(y)); ss[y].insert(find(x)); vec[find(y)].push_back(x); vec[find(x)].push_back(y); } int main() { scanf("%d%d%d%d", &n, &m, &C, &Q); for (int i = 1; i <= n; i++) { par[i] = i; ss[i].insert(i); vec[i].push_back(i); } for (int i = 1; i <= m; i++) { int x, y, c; scanf("%d%d%d", &x, &y, &c); add(x, y, c); } while (Q--) { char opt[5]; int x, y, z; scanf("%s%d%d", opt, &x, &y); if (opt[0] == '+') { scanf("%d", &z); add(x, y, z); } else puts(ss[y].find(find(x)) != ss[y].end() ? "Yes" : "No"); } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx2") using namespace std; const int N = 1e5 + 5; int n, m, c, q; int dsu[N]; map<int, int> edge[N]; set<int> path[N]; int ufds(int u) { return dsu[u] = dsu[u] == u ? u : ufds(dsu[u]); } void join(int u, int v) { u = ufds(u), v = ufds(v); if (u != v) { dsu[v] = u; if (path[u].size() < path[v].size()) { swap(path[u], path[v]); } path[u].insert(path[v].begin(), path[v].end()); } } void create(int u, int v, int cc) { if (edge[u].count(cc)) { join(v, edge[u][cc]); } else { edge[u][cc] = v; } if (edge[v].count(cc)) { join(u, edge[v][cc]); } else { edge[v][cc] = u; } path[ufds(u)].insert(v); path[ufds(v)].insert(u); } int main() { scanf("%d%d%d%d", &n, &m, &c, &q); iota(dsu, dsu + n, 0); for (int i = 0; i < n; i++) { path[i].insert(i); } for (int i = 0; i < m; i++) { int x, y, z; scanf("%d%d%d", &x, &y, &z); x--, y--; create(x, y, z); } for (int i = 0; i < q; i++) { char ch[2]; int x, y; scanf("%s%d%d", ch, &x, &y); x--, y--; if (ch[0] == '+') { int z; scanf("%d", &z); create(x, y, z); } else { puts(path[ufds(x)].count(y) ? "Yes" : "No"); } } }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; map<int, vector<int> > M[N]; int sum[N]; set<int> S[N]; int root(int ary[], int i) { while (ary[i] != i) { ary[i] = ary[ary[i]]; i = ary[i]; } return i; } int un(int Ary[], int a, int b) { int rA = root(Ary, a); int rB = root(Ary, b); if (rA == rB) return 0; if (sum[rA] > sum[rB]) { for (auto it = S[rB].begin(); it != S[rB].end(); it++) { S[rA].insert(*it); } S[rB].clear(); Ary[rB] = rA; sum[rA] += sum[rB]; } else { for (auto it = S[rA].begin(); it != S[rA].end(); it++) { S[rB].insert(*it); } S[rA].clear(); Ary[rA] = rB; sum[rB] += sum[rA]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m, c, q; cin >> n >> m >> c >> q; int ary[n + 1]; for (int i = 0; i < n + 1; ++i) sum[i] = 1; for (int i = 0; i < n + 1; ++i) { ary[i] = i; S[i].insert(i); } for (int i = 0; i < m; ++i) { int a, b, col; cin >> a >> b >> col; M[a][col].push_back(b); M[b][col].push_back(a); S[root(ary, a)].insert(b); S[root(ary, b)].insert(a); un(ary, M[a][col][0], b); un(ary, M[b][col][0], a); } while (q--) { char ch; cin >> ch; if (ch == '?') { int a, b; cin >> a >> b; if (S[root(ary, a)].find(b) != S[root(ary, a)].end()) cout << "Yes" << "\n"; else cout << "No" << "\n"; } else { int a, b, col; cin >> a >> b >> col; M[a][col].push_back(b); M[b][col].push_back(a); S[root(ary, a)].insert(b); S[root(ary, b)].insert(a); un(ary, M[a][col][0], b); un(ary, M[b][col][0], a); } } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename S, typename T> ostream& operator<<(ostream& out, pair<S, T> const& p) { out << '(' << p.first << ", " << p.second << ')'; return out; } template <typename T> ostream& operator<<(ostream& out, vector<T> const& v) { long long l = v.size(); for (long long i = 0; i < l - 1; i++) out << v[i] << ' '; if (l > 0) out << v[l - 1]; return out; } template <typename T> void trace(const char* name, T&& arg1) { cout << name << " : " << arg1 << "\n"; } template <typename T, typename... Args> void trace(const char* names, T&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; trace(comma + 1, args...); } const long long N = 1e5 + 100; long long parent[N]; long long size[N]; long long n, m, c, q; map<long long, set<long long> > comptovert_list, vertex_list; void make_set(long long x) { parent[x] = x; size[x] = 1; vertex_list[x].insert(x); } long long find_set(long long v) { if (v == parent[v]) { return v; } return parent[v] = find_set(parent[v]); } void union_sets(long long a, long long b) { a = find_set(a); b = find_set(b); if (a != b) { if (size[a] < size[b]) { swap(a, b); } parent[b] = a; size[a] += size[b]; for (auto& w : vertex_list[b]) { vertex_list[a].insert(w); } vertex_list[b].clear(); } } map<pair<long long, long long>, long long> comp; void add_edge(long long a, long long b, long long color) { vertex_list[find_set(a)].insert(b); vertex_list[find_set(b)].insert(a); long long t1 = comp[make_pair(a, color)]; long long t2 = comp[make_pair(b, color)]; if (t1 != 0) { union_sets(t1, b); } if (t2 != 0) { union_sets(t2, a); } comp[make_pair(a, color)] = b; comp[make_pair(b, color)] = a; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m >> c >> q; for (long long i = 1; i <= n; i++) { make_set(i); } for (long long i = 0; i < m; i++) { long long x, z, y; cin >> x >> y >> z; add_edge(x, y, z); } for (long long i = 0; i < q; i++) { char s; cin >> s; if (s == '?') { long long x, y; cin >> x >> y; x = find_set(x); if (vertex_list[x].find(y) != vertex_list[x].end()) { cout << "Yes\n"; } else { cout << "No\n"; } } else { long long x, y, z; cin >> x >> y >> z; add_edge(x, y, z); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, m, c, q, qwqwq; int fa[N]; vector<int> G[N], col[N]; set<int> s[N]; map<int, int> mp[N]; char op[4]; bool vis[N]; void addedge(int u, int v, int w) { G[u].push_back(v); col[u].push_back(w); } int find(int u) { if (u == fa[u]) { return fa[u]; } return fa[u] = find(fa[u]); } void uniset(int u, int v) { if (find(u) == find(v)) { return; } if (s[find(u)].size() < s[find(v)].size()) { swap(u, v); } while (!s[find(v)].empty()) { s[find(u)].insert(*s[find(v)].begin()); s[find(v)].erase(s[find(v)].begin()); } fa[find(v)] = find(u); } void dfs(int u) { if (vis[u]) { return; } vis[u] = 1; for (int i = 0, v, w; i < (int)G[u].size(); i++) { v = G[u][i]; w = col[u][i]; if (!mp[u][w]) { mp[u][w] = v; } else { uniset(find(mp[u][w]), find(v)); } } } int main() { scanf("%d%d%d%d", &n, &m, &c, &q); for (int i = 1; i <= n; i++) { s[i].insert(i); fa[i] = i; } for (int i = 1, u, v, w; i <= m; i++) { scanf("%d%d%d", &u, &v, &w); addedge(u, v, w); addedge(v, u, w); s[u].insert(v); s[v].insert(u); } for (int i = 1; i <= n; i++) { dfs(i); } int u, v, w; while (q--) { scanf("%s", op); if (op[0] == '+') { scanf("%d%d%d", &u, &v, &w); s[find(u)].insert(v); s[find(v)].insert(u); if (mp[u][w]) { uniset(find(mp[u][w]), find(v)); } else { mp[u][w] = v; } if (mp[v][w]) { uniset(find(mp[v][w]), find(u)); } else { mp[v][w] = u; } } else { scanf("%d%d", &u, &v); if (s[find(u)].count(v)) { printf("Yes\n"); } else { printf("No\n"); } } } return 0; }
#include <bits/stdc++.h> using namespace std; set<pair<int, int> > *e; int *parent; set<int> *v; int fp(int a) { if (parent[a] != a) parent[a] = fp(parent[a]); return parent[a]; } void dsu(int a, int b) { a = fp(a); b = fp(b); if (a != b) { if (v[a].size() < v[b].size()) swap(a, b); for (set<int>::iterator i = v[b].begin(); i != v[b].end(); i++) v[a].insert(*i); v[b].clear(); parent[b] = a; } } void add(int a, int b, int c) { v[fp(a)].insert(b); v[fp(b)].insert(a); set<pair<int, int> >::iterator t = e[b].lower_bound(make_pair(c, -1)); if ((t == e[b].end()) || (t->first != c)) { e[b].insert(make_pair(c, a)); } else { dsu(a, t->second); } t = e[a].lower_bound(make_pair(c, -1)); if ((t == e[a].end()) || (t->first != c)) { e[a].insert(make_pair(c, b)); } else { dsu(b, t->second); } } int main() { int n, m, c, q; int i; char ch; scanf("%d %d %d %d", &n, &m, &c, &q); parent = new int[100010]; v = new set<int>[100010]; e = new set<pair<int, int> >[100010]; for (i = 0; i < n; i++) { parent[i] = i; } int a, b, cl; while (m--) { scanf("%d %d %d", &a, &b, &cl); add(a, b, cl); } while (q--) { scanf(" %c", &ch); if (ch == '?') { scanf("%d %d", &a, &b); if (fp(a) == fp(b) || (v[fp(a)].find(b) != v[fp(a)].end())) printf("Yes\n"); else printf("No\n"); } else { scanf("%d %d %d", &a, &b, &cl); add(a, b, cl); } } }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int N = 1e5 + 7; const long long MOD = (long long)1e9 + 7; int n, m, c, q, fa[N], sz[N]; map<int, int> G[N]; set<int> ust[N]; void init() { for (int i = 0; i < N; ++i) { fa[i] = i; sz[i] = 1; ust[i].insert(i); } } int findfa(int x) { return fa[x] == x ? fa[x] : fa[x] = findfa(fa[x]); } void mrg(int u, int v, int c) { int tu = G[u][c], tv = G[v][c]; if (tv == 0) { G[v][c] = u; } if (tu == 0) { G[u][c] = v; } ust[findfa(v)].insert(u); ust[findfa(u)].insert(v); int fu, fv; if (tu != 0) { fv = findfa(v); fu = findfa(tu); if (fv != fu) { if (sz[fu] > sz[fv]) swap(fu, fv); sz[fv] += sz[fu]; fa[fu] = fv; for (set<int>::iterator iter = ust[fu].begin(); iter != ust[fu].end(); ++iter) { ust[fv].insert(*iter); } } } if (tv != 0) { fv = findfa(tv); fu = findfa(u); if (fu != fv) { if (sz[fu] > sz[fv]) swap(fu, fv); sz[fv] += sz[fu]; fa[fu] = fv; for (set<int>::iterator iter = ust[fu].begin(); iter != ust[fu].end(); ++iter) { ust[fv].insert(*iter); } } } } int main() { init(); cin >> n >> m >> c >> q; for (int i = 1, u, v, c; i <= m; ++i) { scanf("%d%d%d", &u, &v, &c); mrg(u, v, c); } for (int i = 0, u, v, c; i < q; ++i) { static char op[2]; scanf("%s", op); if (op[0] == '?') { scanf("%d%d", &u, &v); int fu = findfa(u), fv = findfa(v); if (fu == fv) puts("Yes"); else if (ust[fu].find(v) != ust[fu].end()) puts("Yes"); else puts("No"); } else if (op[0] == '+') { scanf("%d%d%d", &u, &v, &c); mrg(u, v, c); } } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> bool umin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> bool umax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } int ata[500009], node; set<int> s[500009]; map<pair<int, int>, int> pm; int tap(int x) { if (!ata[x]) return x; return ata[x] = tap(ata[x]); } void merge(int x, int y) { if ((x = tap(x)) == (y = tap(y))) return; if (s[x].size() < s[y].size()) swap(x, y); ata[y] = x; for (__typeof((s[y]).begin()) it = (s[y]).begin(); it != (s[y]).end(); it++) s[x].insert(*it); s[y].clear(); } int get(int x, int y) { if (!pm[make_pair(x, y)]) pm[make_pair(x, y)] = ++node; return pm[make_pair(x, y)]; } void add(int x, int y, int z) { merge(y, get(x, z)); merge(x, get(y, z)); s[tap(x)].insert(y); s[tap(y)].insert(x); } int main() { int n, m, c, q; scanf("%d%d%d%d", &n, &m, &c, &q); node = n; while (m--) { int u, v, w; scanf("%d%d%d", &u, &v, &w); add(u, v, w); } while (q--) { char c; scanf(" %c", &c); if (c == '+') { int x, y, z; scanf("%d%d%d", &x, &y, &z); add(x, y, z); } else { int x, y; scanf("%d%d", &x, &y); int tmp = tap(x); if (tmp == tap(y) or s[tmp].find(y) != s[tmp].end()) puts("Yes"); else puts("No"); } } return 0; }
#include <bits/stdc++.h> using namespace std; int fa[100010], n, m, c, q; set<int> s[100010]; map<int, vector<int> > mp[100010]; int fd(int x) { return fa[x] == x ? x : fa[x] = fd(fa[x]); } void mg(int x, int y) { int a = fd(x), b = fd(y); if (a != b) { if (s[a].size() < s[b].size()) swap(a, b); for (auto i : s[b]) s[a].insert(i); fa[b] = a; } } void add() { int x, y, c; scanf("%d%d%d", &x, &y, &c); s[fd(x)].insert(y); s[fd(y)].insert(x); mp[x][c].push_back(y); mp[y][c].push_back(x); mg(mp[x][c][0], y); mg(mp[y][c][0], x); } int main() { scanf("%d%d%d%d", &n, &m, &c, &q); for (int i = 1; i <= n; i++) fa[i] = i; while (m--) add(); while (q--) { char c[5]; int x, y; scanf("%s", c); if (c[0] == '?') { scanf("%d%d", &x, &y); puts((s[fd(x)].count(y) || fd(x) == fd(y)) ? "Yes" : "No"); } else add(); } }
#include <bits/stdc++.h> using namespace std; int n, m, c, q; vector<int> e[100007]; int F[100007]; int Find(int x) { return F[x] == x ? x : F[x] = Find(F[x]); } set<int> S[100007], s[100007]; int idx; map<int, int> id[100007]; int a[100007 * 4], b[100007 * 4], f[100007 * 4], g[100007 * 4]; int find(int x) { if (f[x] == x) return x; int fx = find(f[x]); g[x] ^= g[f[x]]; return f[x] = fx; } void join(int x, int y) { int fx = Find(x), fy = Find(y); if (fx == fy) return; if (S[fx].size() > S[fy].size()) swap(fx, fy); for (auto u : S[fx]) { S[fy].insert(u); for (int v : e[u]) s[v].erase(fx), s[v].insert(fy); } F[fx] = fy; } void add(int x, int y, int z) { if (id[z].find(x) == id[z].end()) { id[z][x] = ++idx; a[idx] = x, b[idx] = 0; g[f[idx] = idx] = 0; } if (id[z].find(y) == id[z].end()) { id[z][y] = ++idx; a[idx] = y, b[idx] = 0; g[f[idx] = idx] = 0; } int zx = id[z][x], zy = id[z][y]; int fzx = find(zx), fzy = find(zy); if (fzx != fzy) { f[fzx] = fzy, g[fzx] = g[zx] ^ g[zy] ^ 1; if (!b[fzy]) b[fzy] = a[g[zy] ? zy : zx]; if (!g[fzx]) { join(a[fzx], a[fzy]); if (b[fzx]) join(b[fzx], b[fzy]); } else { join(a[fzx], b[fzy]); if (b[fzx]) join(b[fzx], a[fzy]); } } else if (g[zx] ^ g[zy] ^ 1) join(b[fzx], a[fzx]); e[x].push_back(y), s[x].insert(Find(y)); e[y].push_back(x), s[y].insert(Find(x)); } int main() { scanf("%d%d%d%d", &n, &m, &c, &q); for (int i = 1; i <= n; ++i) F[i] = i, S[i].insert(i); while (m--) { int x, y, z; scanf("%d%d%d", &x, &y, &z); add(x, y, z); } while (q--) { char o = getchar(); while (o != '+' && o != '?') o = getchar(); int x, y, z; if (o == '+') { scanf("%d%d%d", &x, &y, &z); add(x, y, z); } else { scanf("%d%d", &x, &y); if (Find(x) == Find(y) || s[y].find(Find(x)) != s[y].end()) puts("Yes"); else puts("No"); } } }
#include <bits/stdc++.h> using namespace std; using ll = long long; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1e9 + 7; struct UnionFind { vector<int> par; vector<set<int>> st; UnionFind(int n) : par(n, -1), st(n) {} int find(int x) { if (par[x] < 0) return x; return par[x] = find(par[x]); } bool unite(int x, int y) { x = find(x); y = find(y); if (x == y) return false; if (par[x] > par[y]) { par[y] += par[x]; par[x] = y; for (auto e : st[x]) st[y].insert(e); } else { par[x] += par[y]; par[y] = x; for (auto e : st[y]) st[x].insert(e); } return true; } bool same(int x, int y) { if (find(x) == find(y)) return true; if (st[find(x)].count(y)) return true; return false; } int size(int x) { return -par[find(x)]; } }; int main() { int n, m, c, q; cin >> n >> m >> c >> q; map<pair<int, int>, vector<int>> edge; UnionFind uf(n + 1); for (int i = (int)(0); i < (int)(m); ++i) { int x, y, z; cin >> x >> y >> z; uf.st[x].insert(y); uf.st[y].insert(x); edge[{x, z}].push_back(y); edge[{y, z}].push_back(x); } for (auto e : edge) { if (e.second.size() >= 2) { for (int i = (int)(0); i < (int)(e.second.size()); ++i) { uf.unite(e.second[0], e.second[i]); } } } while (q--) { char op; cin >> op; if (op == '+') { int x, y, z; cin >> x >> y >> z; edge[{x, z}].push_back(y); edge[{y, z}].push_back(x); uf.st[uf.find(y)].insert(x); uf.st[uf.find(x)].insert(y); uf.unite(y, edge[{x, z}][0]); uf.unite(x, edge[{y, z}][0]); } else { int x, y; cin >> x >> y; cout << (uf.same(x, y) ? "Yes\n" : "No\n"); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; const int maxm = 5e6 + 10; const long long mod = 1e9 + 7; map<int, int> ma[maxn]; set<int> se[maxn]; int n, m, c, q, boss[maxn]; void inti() { for (int i = 1; i <= n; i++) boss[i] = i; } int fin(int x) { if (boss[x] == x) return x; return boss[x] = fin(boss[x]); } void uni(int a, int b) { int v1 = fin(a), v2 = fin(b); if (v1 == v2) return; if (se[v1].size() > se[v2].size()) swap(v1, v2); boss[v1] = v2; for (auto i : se[v1]) se[v2].insert(i); se[v1].clear(); } void add_edge() { int a, b, v; scanf("%d %d %d", &a, &b, &v); if (ma[a].count(v)) uni(ma[a][v], b); else ma[a][v] = b; if (ma[b].count(v)) uni(ma[b][v], a); else ma[b][v] = a; se[fin(a)].insert(b); se[fin(b)].insert(a); } int main() { scanf("%d %d %d %d", &n, &m, &c, &q); inti(); for (int i = 1; i <= m; i++) add_edge(); while (q--) { getchar(); if (getchar() == '?') { int fla = 0, a, b; scanf("%d %d", &a, &b); int v = fin(a); if (v == fin(b) || se[v].find(b) != se[v].end()) printf("Yes\n"); else printf("No\n"); } else { add_edge(); } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, c, q; unordered_map<int, unordered_map<int, int>> mp; vector<unordered_set<int>> S, rS; struct DSU { int n; vector<int> f, sz; DSU(int n_) { n = n_; f.resize(n); sz.resize(n); S.resize(n); rS.resize(n); for (int i = 0; i < n; ++i) { f[i] = i; sz[i] = 1; S[i] = {i}; rS[i] = {i}; } } int find_father(int a) { return f[a] == a ? a : (f[a] = this->find_father(f[a])); } inline int get_size(int i) { return sz[this->find_father(i)]; } bool connected(int a, int b) { return this->find_father(a) == this->find_father(b); } void connect(int a, int b) { if (connected(a, b)) return; a = f[a]; b = f[b]; if (sz[a] > sz[b]) swap(a, b); sz[b] += sz[a]; f[a] = b; for (auto e : rS[a]) { S[e].erase(a); S[e].insert(b); rS[b].insert(e); } rS[a].clear(); } }; DSU dsu(1); void insert_edge() { int x, y, z; cin >> x >> y >> z; for (auto u : {x, y}) if (mp[u][z] == 0) { mp[u][z] = x + y - u; } else dsu.connect(mp[u][z], x + y - u); S[x].insert(dsu.find_father(y)); rS[dsu.find_father(y)].insert(x); S[y].insert(dsu.find_father(x)); rS[dsu.find_father(x)].insert(y); } int main() { cin >> n >> m >> c >> q; dsu = DSU(n + 1); while (m--) { insert_edge(); } while (q--) { char ch; cin >> ch; if (ch == '+') { insert_edge(); } if (ch == '?') { int x, y; cin >> x >> y; cout << (S[y].find(dsu.find_father(x)) != S[y].end() ? "Yes" : "No") << "\n"; } } }
#include <bits/stdc++.h> using namespace std; int p[300009], s[300009]; unordered_map<int, int> A[300009]; unordered_set<int> S[300009], Z[300009]; int W[300009]; void UN(int x, int y) { if (Z[W[x]].size() > Z[W[y]].size()) swap(x, y); for (auto it = Z[W[x]].begin(); it != Z[W[x]].end(); it++) { Z[W[y]].insert(*it); S[*it].insert(W[y]); S[*it].erase(S[*it].find(W[x])); } Z[W[x]].clear(); W[x] = W[y]; } int P(int x) { if (p[x] != x) p[x] = P(p[x]); return p[x]; } void Un(int x, int y) { x = P(x); y = P(y); if (x == y) return; if (s[x] > s[y]) swap(x, y); p[x] = y; s[y] += x; UN(x, y); } void ad(int a, int b, int c) { if (A[a][c]) Un(A[a][c], b); else A[a][c] = b; if (A[b][c]) Un(A[b][c], a); else A[b][c] = a; S[a].insert(W[P(b)]); S[b].insert(W[P(a)]); Z[W[P(b)]].insert(a); Z[W[P(a)]].insert(b); } int main() { int n, m, c, q; cin >> n >> m >> c >> q; for (int i = 1; i <= n; i++) p[i] = i, s[i] = 1, W[i] = i; for (int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; ad(a, b, c); } while (q--) { char s; cin >> s; if (s == '?') { int x, y; cin >> x >> y; if (P(x) == P(y)) cout << "Yes" << endl; else if (S[y].find(W[P(x)]) != S[y].end()) cout << "Yes" << endl; else cout << "No" << endl; } else { int x, y, z; cin >> x >> y >> z; ad(x, y, z); } } }
#include <bits/stdc++.h> using namespace std; struct Set { int f[100005]; set<int> g[100005]; void init(int n) { for (int i = 1; i <= n; i++) f[i] = i; } int get_parent(int i) { if (f[i] == i) return f[i]; f[i] = get_parent(f[i]); return f[i]; } bool in_set(int i, int j) { return get_parent(i) == get_parent(j); } void combine_e(int i, int j) { if (in_set(i, j)) return; f[f[j]] = f[i]; } void combine(int i, int j) { if (in_set(i, j)) return; if (g[f[i]].size() < g[f[j]].size()) swap(i, j); set<int>::iterator it = g[f[j]].begin(); for (; it != g[f[j]].end(); it++) { g[f[i]].insert(*it); } g[f[j]].clear(); f[f[j]] = f[i]; } } s; struct adj { int size; int head[100005]; int next[100005 << 2]; int node[100005 << 2]; int edge[100005 << 2]; void add_edge(int x, int y, int c) { ++size; next[size] = head[x]; node[size] = y; edge[size] = c; head[x] = size; } } a; int cl[100005]; unordered_map<long long, int> h; inline long long unique_value(long long x, long long z) { return x * 100005 + z; } void connect_r(int n) { s.init(n); for (int u = 1; u <= n; u++) { for (int i = a.head[u]; i; i = a.next[i]) { if (!cl[a.edge[i]]) { cl[a.edge[i]] = a.node[i]; h.insert(make_pair(unique_value(u, a.edge[i]), a.node[i])); } else s.combine_e(cl[a.edge[i]], a.node[i]); } for (int i = a.head[u]; i; i = a.next[i]) cl[a.edge[i]] = 0; } for (int u = 1; u <= n; u++) { int p = s.get_parent(u); for (int i = a.head[u]; i; i = a.next[i]) { s.g[p].insert(a.node[i]); } } } void update(int x, int y, int z) { unordered_map<long long, int>::iterator i; long long p = unique_value(x, z); i = h.find(p); if (i == h.end()) h.insert(make_pair(p, y)); else s.combine(y, i->second); p = unique_value(y, z); i = h.find(p); if (i == h.end()) h.insert(make_pair(p, x)); else s.combine(x, i->second); a.add_edge(x, y, z); a.add_edge(y, x, z); s.g[s.get_parent(x)].insert(y); s.g[s.get_parent(y)].insert(x); } bool enquiry(int x, int y) { if (s.in_set(x, y)) return true; int p = s.get_parent(x); if (s.g[p].find(y) != s.g[p].end()) return true; return false; } int n, m, c, q; int main() { scanf("%d%d%d%d", &n, &m, &c, &q); for (int i = 0; i < m; i++) { int x, y, z; scanf("%d%d%d", &x, &y, &z); a.add_edge(x, y, z); a.add_edge(y, x, z); } connect_r(n); char o; for (int i = 0; i < q; i++) { int x, y; cin >> o; scanf("%d%d", &x, &y); if (o == '?') { if (enquiry(x, y)) printf("Yes\n"); else printf("No\n"); } else { int z; scanf("%d", &z); update(x, y, z); } } return 0; }
#include <bits/stdc++.h> const double esp = 1e-6; const double pi = acos(-1.0); const int INF = 0x3f3f3f3f; const int inf = 1e9; using namespace std; int read() { char ch = getchar(); int x = 0, f = 1; while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } const int N = 1e5 + 10; map<int, int> mp[N]; set<int> s[N]; int pr[N]; int n, m, c, q; int x, y, z; char ss[2]; void init() { for (int i = 1; i <= n; i++) pr[i] = i; } int find(int x) { return pr[x] == x ? x : pr[x] = find(pr[x]); } void unit(int a, int b) { a = find(a); b = find(b); if (a == b) return; if (s[a].size() < s[b].size()) swap(a, b); pr[b] = a; for (auto i : s[b]) s[a].insert(i); s[b].clear(); } void mk(int x, int y, int z) { if (mp[x][z]) { unit(mp[x][z], y); } mp[x][z] = y; if (mp[y][z]) { unit(mp[y][z], x); } mp[y][z] = x; s[find(x)].insert(y); s[find(y)].insert(x); } int main() { n = read(), m = read(), c = read(), q = read(); init(); for (int i = 0; i < m; i++) { x = read(); y = read(); z = read(); mk(x, y, z); } while (q--) { cin >> ss; if (ss[0] == '?') { x = read(); y = read(); x = find(x); if (x == find(y) || s[x].find(y) != s[x].end()) { cout << "Yes" << endl; } else cout << "No" << endl; } else { x = read(); y = read(); z = read(); mk(x, y, z); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; const int MAXV = MAXN * 5; const long long INF = (long long)1e9 + 5; const long long MOD = 1000000007; struct DSU { int pa[MAXV]; set<int> member[MAXV]; void init() { for (int i = 0; i < MAXV; i++) { pa[i] = i; member[i].insert(i); } } int find(int x) { return pa[x] == x ? x : pa[x] = find(pa[x]); } void merge(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (member[y].size() > member[x].size()) swap(x, y); for (int v : member[y]) member[x].insert(v); member[y].clear(); pa[y] = x; } } dsu; struct Query { char t; int x, y, z; }; int N, M, Q, C, st[MAXN], who[MAXV]; vector<int> col[MAXN]; vector<int> G[MAXV]; Query qs[MAXN * 2]; int get_id(int v, int c) { int pos = lower_bound(col[v].begin(), col[v].end(), c) - col[v].begin(); assert(pos != col[v].size() && col[v][pos] == c); return st[v] + pos; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N >> M >> C >> Q; Q += M; for (int i = 0; i < M; i++) { int x, y, c; cin >> x >> y >> c; col[x].push_back(c); col[y].push_back(c); qs[i] = {'+', x, y, c}; } for (int i = M; i < Q; i++) { char t; int x, y, c = -1; cin >> t >> x >> y; if (t == '+') { cin >> c; col[x].push_back(c); col[y].push_back(c); } qs[i] = {t, x, y, c}; } st[1] = 1; for (int i = 1; i <= N; i++) { col[i].push_back(0); sort(col[i].begin(), col[i].end()); col[i].resize(unique(col[i].begin(), col[i].end()) - col[i].begin()); st[i + 1] = st[i] + col[i].size(); for (int j = st[i]; j < st[i + 1]; j++) { who[j] = i; } } dsu.init(); for (int _q = 0; _q < Q; _q++) { char t = qs[_q].t; int x = qs[_q].x, y = qs[_q].y, c = qs[_q].z; if (t == '+') { dsu.merge(get_id(x, 0), get_id(y, c)); dsu.merge(get_id(x, c), get_id(y, 0)); } else { int v = dsu.find(get_id(x, 0)); auto it = dsu.member[v].lower_bound(st[y]); bool ok = it != dsu.member[v].end() && st[y] <= *it && *it < st[y + 1]; cout << (ok ? "Yes\n" : "No\n"); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 500050; const int inf = 0x3f3f3f3f; const long long llinf = 0x3f3f3f3f3f3f3f3f; const int mod = 998244353; const int MOD = 1e9 + 7; const double PI = acos(-1.0); clock_t TIME__START, TIME__END; void program_end() {} int n, m, c, q; set<int> s[N]; map<int, int> ecol[N]; int f[N]; int Find(int x) { return x == f[x] ? x : f[x] = Find(f[x]); } bool connect(int x, int y) { return ((x = Find(x)) ^ (y = Find(y))) ? f[y] = x, 1 : 0; } void inif(int n) { for (int i = 1; i <= n; ++i) f[i] = i; } void uni(int x, int y) { x = Find(x), y = Find(y); if (x == y) return; if (s[x].size() > s[y].size()) swap(x, y); f[x] = y; for (auto i : s[x]) s[y].insert(i); s[x].clear(); } void add(int x, int y, int z) { if (ecol[x].count(z)) uni(ecol[x][z], y); else ecol[x][z] = y; if (ecol[y].count(z)) uni(ecol[y][z], x); else ecol[y][z] = x; int fx = Find(x), fy = Find(y); s[fx].insert(y), s[fy].insert(x); } inline void solve() { cin >> n >> m >> c >> q; inif(n + 5); for (int i = 1; i <= m; ++i) { int x, y, z; cin >> x >> y >> z; add(x, y, z); } while (q--) { char op[5]; int x, y, z; cin >> op >> x >> y; if (op[0] == '?') { x = Find(x); if (x == Find(y) || s[x].find(y) != s[x].end()) puts("Yes"); else puts("No"); } else { cin >> z; add(x, y, z); } } } int main() { ios::sync_with_stdio(false), cin.tie(NULL); TIME__START = clock(); int Test = 1; while (Test--) { solve(); } TIME__END = clock(); program_end(); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 100; int n, m, c, q, u, v, par[N]; char type; vector<int> nei[N], vec[N]; set<int> st[N]; map<pair<int, int>, int> con; void merge(int u, int v) { int U = par[u], V = par[v]; if (U == V) return; if (vec[U].size() > vec[V].size()) swap(U, V); for (int x : vec[U]) { for (int y : nei[x]) st[y].insert(V); vec[V].push_back(x); par[x] = V; } } void add_edge(int u, int v, int c) { nei[u].push_back(v); nei[v].push_back(u); st[u].insert(par[v]); st[v].insert(par[u]); pair<int, int> p = pair<int, int>(u, c), q = pair<int, int>(v, c); if (con[p] == 0) con[p] = v; else merge(v, con[p]); if (con[q] == 0) con[q] = u; else merge(u, con[q]); } int main() { ios::sync_with_stdio(false), cin.tie(0); iota(par, par + N, 0); for (int i = 0; i < N; i++) vec[i].push_back(i); cin >> n >> m >> c >> q; while (m--) { cin >> u >> v >> c; add_edge(u, v, c); } while (q--) { cin >> type; if (type == '+') { cin >> u >> v >> c; add_edge(u, v, c); } else { cin >> u >> v; cout << (par[u] == par[v] || st[v].find(par[u]) != st[v].end() ? "Yes" : "No") << '\n'; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2000005; set<int> s[N]; map<int, int> mp[N]; int fa[N], n, m, c, q; char opt[3]; int finds(int x) { if (fa[x] == x) return fa[x]; fa[x] = finds(fa[x]); return fa[x]; } void merge(int x, int y) { int fx = finds(x), fy = finds(y); if (fx == fy) return; if (s[fx].size() < s[fy].size()) swap(fx, fy); fa[fy] = fx; for (int i : s[fy]) s[fx].insert(i); s[fy].clear(); } void addedge(int x, int y, int col) { s[finds(x)].insert(y); if (mp[x].count(col)) { int v = mp[x][col]; merge(y, v); } else mp[x][col] = y; } int main() { scanf("%d%d%*d%d", &n, &m, &q); for (int i = 1; i <= n; ++i) fa[i] = i; for (int i = 1; i <= m; ++i) { int x, y, z; scanf("%d%d%d", &x, &y, &z); addedge(x, y, z); addedge(y, x, z); } for (int i = 1; i <= q; ++i) { scanf("%s", opt); int x, y; scanf("%d%d", &x, &y); if (opt[0] == '+') { int z; scanf("%d", &z); addedge(x, y, z); addedge(y, x, z); } else { int fx = finds(x), fy = finds(y); if ((fx == fy) || (s[fx].count(y))) { puts("Yes"); } else puts("No"); } } return 0; }
#include <bits/stdc++.h> using namespace std; template <class A, class B> ostream& operator<<(ostream& out, const pair<A, B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template <class A> ostream& operator<<(ostream& out, const vector<A>& v) { out << "["; for (int i = 0; i < v.size(); ++i) { if (i) out << ", "; out << v[i]; } return out << "]"; } const int N = 1e5 + 6; const string yes = "Yes", no = "No"; vector<vector<pair<int, int>>> adj(N); vector<int> par(N); vector<set<int>> sets(N); vector<map<int, int>> adj1(N); int parent(int v) { if (par[v] == v) return v; else return par[v] = parent(par[v]); } void merge(int from, int to) { if (from == to) return; from = parent(from); to = parent(to); if (sets[to].size() > sets[from].size()) { sets[to].insert(sets[from].begin(), sets[from].end()); set<int>().swap(sets[from]); } else { sets[from].insert(sets[to].begin(), sets[to].end()); set<int>().swap(sets[to]); sets[from].swap(sets[to]); } } void dsu(int u, int v) { merge(parent(u), parent(v)); par[u] = parent(par[v]); } void add(int x, int y, int color) { if (adj1[y][color]) { dsu(parent(x), parent(adj1[y][color])); } if (adj1[x][color]) { dsu(parent(y), parent(adj1[x][color])); } adj1[x][color] = y; adj1[y][color] = x; sets[parent(x)].insert(y); sets[parent(y)].insert(x); } int main(int argc, char const* argv[]) { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, m, c, q; cin >> n >> m >> c >> q; iota(par.begin(), par.end(), 0); for (int i = (0); i < (m); ++i) { int x, y, z; cin >> x >> y >> z; add(x, y, z); } for (int i = (0); i < (q); ++i) { char c; int x, y, z; cin >> c; if (c == '+') { cin >> x >> y >> z; add(x, y, z); } else { cin >> x >> y; if (parent(x) == parent(y)) { cout << yes << endl; } else if (sets[parent(x)].count(y) == 1) { cout << yes << endl; } else { cout << no << endl; } } } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; unordered_map<int, vector<int> > G[100005]; int p[100005]; unordered_set<int> s[100005]; void init(int n) { for (int i = 2; i <= n; i++) p[i] = i; } int find(int x) { if (p[x] == x) return x; return p[x] = find(p[x]); } void unite(int x, int y) { int rx = find(x), ry = find(y); if (s[rx].size() > s[ry].size()) swap(rx, ry); for (auto it : s[rx]) s[ry].insert(it); p[rx] = ry; } bool same(int u, int v) { return find(u) == find(v); } void _add_edge(int u, int v, int c, bool i = false) { auto& a = G[u][c]; a.push_back(v); if (a.size() >= 2) unite(a[0], v); if (!i) s[find(u)].insert(v); } void add_edge(int u, int v, int c, bool i = false) { _add_edge(u, v, c, i); _add_edge(v, u, c, i); } int main() { std::ios::sync_with_stdio(0); std::cin.tie(0); ; int n, m, c, q; cin >> n >> m >> c >> q; init(n); for (int i = 0; i < m; i++) { int u, v, cc; cin >> u >> v >> cc; add_edge(u, v, cc, 1); } for (int i = 1; i <= n; i++) for (auto it = G[i].begin(); it != G[i].end(); ++it) for (int j : it->second) s[find(i)].insert(j); while (q--) { char op; cin >> op; if (op == '+') { int u, v, cc; cin >> u >> v >> cc; add_edge(u, v, cc); } else { int u, v; cin >> u >> v; if (same(u, v) || s[find(u)].count(v)) { cout << "Yes" << '\n'; } else { cout << "No" << '\n'; } } } }
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, m, c, q, u, v, cnt = 0; char t; map<pair<int, int>, int> ind; vector<int> dsu; vector<set<int>> odd; int get(int u, int c) { if (ind.count({u, c}) == 0) { ind[{u, c}] = cnt++; dsu.push_back(-1); odd.push_back({}); } return ind[{u, c}]; } int trace(int u) { return dsu[u] < 0 ? u : dsu[u] = trace(dsu[u]); } void connect(int u, int v) { if ((u = trace(u)) == (v = trace(v))) return; if (dsu[u] > dsu[v]) swap(u, v); dsu[u] += dsu[v]; dsu[v] = u; if (odd[u].size() < odd[v].size()) swap(odd[u], odd[v]); for (int ele : odd[v]) odd[u].insert(ele); odd[v].clear(); } void add(int u, int v) { u = trace(u); odd[u].insert(v); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m >> c >> q; while (m--) { cin >> u >> v >> c; int l = get(u, c), r = get(v, c); add(l, u); add(r, v); connect(l, get(v, 0)); connect(r, get(u, 0)); } while (q--) { cin >> t >> u >> v; if (t == '?') { if (trace(get(u, 0)) == trace(get(v, 0))) cout << "Yes\n"; else if (odd[trace(get(u, 0))].count(v) == 1) cout << "Yes\n"; else cout << "No\n"; } else { cin >> c; int l = get(u, c), r = get(v, c); add(l, u); add(r, v); connect(l, get(v, 0)); connect(r, get(u, 0)); } } }
#include <bits/stdc++.h> using namespace std; struct Node { map<int, vector<int>> adj; }; struct DisjointSets { vector<int> parent; vector<set<int>> fuzzy; DisjointSets(const int N) : parent(N), fuzzy(N) { for (int i = 0; i < N; ++i) { parent[i] = i; fuzzy[i].insert(i); } } int find(int i) { int& pi = parent[i]; if (pi == i) { return i; } int result = find(pi); pi = result; return result; } void link(int i, int j) { int irep = find(i); int jrep = find(j); if (irep != jrep) { int irank(fuzzy[irep].size()), jrank(fuzzy[jrep].size()); if (irank <= jrank) { parent[irep] = jrep; for (auto it = fuzzy[irep].begin(); it != fuzzy[irep].end(); ++it) { fuzzy[jrep].insert(*it); } } else { parent[jrep] = irep; for (auto it = fuzzy[jrep].begin(); it != fuzzy[jrep].end(); ++it) { fuzzy[irep].insert(*it); } } } } void fuzzy_link(int i, int j) { int irep = find(i); int jrep = find(j); if (irep != jrep) { fuzzy[irep].insert(j); fuzzy[jrep].insert(i); } } bool connected(const int u, const int v) { int urep = find(u); return fuzzy[urep].count(v); } }; struct Solver { const int N; vector<Node> nodes; DisjointSets ds; Solver(const int n) : N(n), nodes(n), ds(n) {} void add(const int u, const int v, const int c) { if (nodes[u].adj[c].size()) { ds.link(v, nodes[u].adj[c].front()); } if (nodes[v].adj[c].size()) { ds.link(u, nodes[v].adj[c].front()); } ds.fuzzy_link(u, v); ds.fuzzy_link(v, u); nodes[u].adj[c].push_back(v); nodes[v].adj[c].push_back(u); } bool has_path(const int u, const int v) { return ds.connected(u, v); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int M, N, C, Q; cin >> N >> M >> C >> Q; Solver solver(N); for (int i = 0; i < M; ++i) { int u, v, c; cin >> u >> v >> c; --u; --v; solver.add(u, v, c); } for (int q = 0; q < Q; ++q) { char type; cin >> type; if (type == '+') { int x, y, z; cin >> x >> y >> z; --x; --y; solver.add(x, y, z); } else { int x, y; cin >> x >> y; --x; --y; bool ans = solver.has_path(x, y); cout << (ans ? "Yes\n" : "No\n"); } } return 0; }
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0; int ch = getchar(), f = 1; while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar(); if (ch == '-') { f = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } return x * f; } const int N = 5e5 + 10; int n, m, c, q, x, y, z; char opt[20]; int f[N]; set<int> s[N]; inline void init() { for (register int i = (1); i <= (5e5); i++) f[i] = i; } inline int Find(int x) { return f[x] == x ? x : f[x] = Find(f[x]); } inline void Merge(int x, int y) { x = Find(x), y = Find(y); if (x != y) { if (s[x].size() < s[y].size()) swap(x, y); f[y] = x; for (auto v : s[y]) s[x].insert(v); } } inline int Query(int x, int y) { x = Find(x); if (x == Find(y) || s[x].find(y) != s[x].end()) return 1; else return 0; } int cnt; map<int, int> pos[N]; inline void Add(int x, int y, int z) { if (!pos[x].count(z)) pos[x][z] = ++cnt; Merge(y, pos[x][z]); if (!pos[y].count(z)) pos[y][z] = ++cnt; Merge(x, pos[y][z]); s[Find(x)].insert(y), s[Find(y)].insert(x); } int main() { n = cnt = read(), m = read(), c = read(), q = read(); init(); for (register int i = (1); i <= (m); i++) x = read(), y = read(), z = read(), Add(x, y, z); while (q--) { scanf("%s", opt); if (opt[0] == '+') x = read(), y = read(), z = read(), Add(x, y, z); else x = read(), y = read(), puts(Query(x, y) ? "Yes" : "No"); } }
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void smin(T &a, const U &b) { if (a > b) a = b; } template <typename T, typename U> inline void smax(T &a, const U &b) { if (a < b) a = b; } template <class T> inline void gn(T &first) { char c, sg = 0; while (c = getchar(), (c > '9' || c < '0') && c != '-') ; for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9'; c = getchar()) first = (first << 1) + (first << 3) + c - '0'; if (sg) first = -first; } template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) { gn(x1), gn(x2); } int power(int a, int b, int m, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } int root[100100]; int find(int u) { return root[u] == u ? u : root[u] = find(root[u]); } unordered_map<int, int> adj[100100]; set<int> con[100100]; void add_edge(int u, int v, int c) { if (adj[u].count(c)) { int first = find(v); int second = find(adj[u][c]); if (first != second) { if (con[first].size() < con[second].size()) swap(first, second); for (auto z : con[second]) con[first].insert(z); root[second] = first; } } else adj[u][c] = v; con[find(v)].insert(u); } int main() { int n, m, c, q; gn(n, m), gn(c, q); for (int i = 1; i <= n; i++) root[i] = i; for (int i = 1; i <= m; i++) { int u, v, c; gn(u, v); gn(c); add_edge(u, v, c); add_edge(v, u, c); } char tp; int u, v; while (q--) { scanf(" %c", &tp); gn(u, v); if (tp == '+') { gn(c); add_edge(u, v, c); add_edge(v, u, c); } else { if (find(u) == find(v) || con[find(u)].count(v)) puts("Yes"); else puts("No"); } } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> using v2d = vector<vector<T> >; template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); const int maxN = 1e5 + 10; int n, m, c, q, p[maxN]; set<int> st[maxN]; map<int, int> mp[maxN]; int root(int u) { return u == p[u] ? u : p[u] = root(p[u]); } bool unify(int u, int v) { u = root(u); v = root(v); if (u == v) { return 0; } if (st[u].size() < st[v].size()) { swap(u, v); } p[v] = u; st[u].insert((st[v]).begin(), (st[v]).end()); st[v].clear(); return 1; } void solve() { cin >> n >> m >> c >> q; iota(p + 1, p + n + 1, 1); auto add_edge = [&](int u, int v, int col) { if (mp[u].count(col)) { unify(mp[u][col], v); } mp[u][col] = v; st[root(u)].insert(v); }; while (m--) { int x, y, z; cin >> x >> y >> z; add_edge(x, y, z); add_edge(y, x, z); } while (q--) { char type; cin >> type; if (type == '+') { int x, y, z; cin >> x >> y >> z; add_edge(x, y, z); add_edge(y, x, z); } else { int x, y; cin >> x >> y; cout << (root(x) == root(y) || st[root(x)].count(y) ? "Yes" : "No") << "\n"; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int T = 1; while (T--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; set<int> vertex_list[N], comp_list[N]; map<pair<int, int>, int> edges; int sz[N], par[N]; void add_edge(int u, int v, int c); int get_root(int u); void merge(int u, int v); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, t, q; cin >> n >> m >> t >> q; for (int i = 1; i <= n; ++i) { sz[i] = 1; par[i] = i; } while (m--) { int u, v, c; cin >> u >> v >> c; add_edge(u, v, c); } while (q--) { char type; cin >> type; if (type == '+') { int u, v, c; cin >> u >> v >> c; add_edge(u, v, c); } else { int u, v; cin >> u >> v; u = get_root(u); if (u == get_root(v) or comp_list[v].find(u) != comp_list[v].end()) { cout << "Yes\n"; } else { cout << "No\n"; } } } } void add_edge(int u, int v, int c) { int c1 = edges[{u, c}]; int c2 = edges[{v, c}]; edges[{u, c}] = v; edges[{v, c}] = u; if (c1) { merge(c1, v); } if (c2) { merge(c2, u); } comp_list[u].insert(get_root(v)); comp_list[v].insert(get_root(u)); vertex_list[get_root(u)].insert(v); vertex_list[get_root(v)].insert(u); } int get_root(int u) { if (par[u] != u) { par[u] = get_root(par[u]); } return par[u]; } void merge(int u, int v) { u = get_root(u); v = get_root(v); if (u == v) { return; } if (sz[u] < sz[v]) { swap(u, v); } sz[u] += sz[v]; par[v] = u; for (auto &w : vertex_list[v]) { comp_list[w].erase(v); comp_list[w].insert(u); vertex_list[u].insert(w); } vertex_list[v].clear(); }
#include <bits/stdc++.h> using namespace std; int ds[100100]; set<int> neighbors[100100]; map<pair<int, int>, int> coloredNode; int get(int x) { return ds[x] == x ? x : ds[x] = get(ds[x]); } void join(int x, int y) { int dx = get(x), dy = get(y); if (dx != dy) { if (neighbors[dx].size() < neighbors[dy].size()) swap(dx, dy); for (int u : neighbors[dy]) neighbors[dx].insert(u); ds[dy] = dx; } } void addEdge(int x, int y, int z) { neighbors[get(x)].insert(y); if (!coloredNode.count({y, z})) coloredNode[{y, z}] = x; else join(coloredNode[{y, z}], x); } string query(int x, int y) { int dx = get(x), dy = get(y); return dx == dy || neighbors[dx].count(y) ? "Yes" : "No"; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, c, q, x, y, z; cin >> n >> m >> c >> q; for (int i = 1; i <= n; i++) { ds[i] = i; neighbors[i].insert(i); } while (m--) { cin >> x >> y >> z; addEdge(x, y, z); addEdge(y, x, z); } while (q--) { char t; cin >> t >> x >> y; if (t == '?') cout << query(x, y) << '\n'; else { cin >> z; addEdge(x, y, z); addEdge(y, x, z); } } }
#include <bits/stdc++.h> using namespace std; int N, M, C, Q, K; set<int> neigh[1000005]; map<pair<int, int>, int> mp; int par[1000005]; int getrt(int n) { return par[n] = (par[n] == n ? n : getrt(par[n])); } void unite(int a, int b) { a = getrt(a), b = getrt(b); if (a != b) { if (neigh[a].size() > neigh[b].size()) { swap(neigh[b], neigh[a]); } for (int n : neigh[a]) { neigh[b].insert(n); } par[a] = b; } } void addedge(int a, int b, int c) { if (!mp.count({a, c})) { mp[{a, c}] = ++K; par[K] = K; } if (!mp.count({b, c})) { mp[{b, c}] = ++K; par[K] = K; } neigh[getrt(a)].insert(b); neigh[getrt(b)].insert(a); unite(a, mp[{b, c}]); unite(b, mp[{a, c}]); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N >> M >> C >> Q; K = N; for (int i = 1; i <= N; i++) { par[i] = i; } for (int i = 1; i <= M; i++) { int a, b, c; cin >> a >> b >> c; addedge(a, b, c); } while (Q--) { char c; cin >> c; if (c == '?') { int a, b; cin >> a >> b; a = getrt(a); if (getrt(b) == a || neigh[a].count(b)) { cout << "Yes\n"; } else { cout << "No\n"; } } else { int a, b, c; cin >> a >> b >> c; addedge(a, b, c); } } }
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void debug(const char* name, Arg1&& arg1) { cerr << name << ": " << arg1 << '\n'; } template <typename Arg1, typename... Args> void debug(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << ": " << arg1 << " | "; debug(comma + 1, args...); } const int INF = 1000000000 + 5; const int N = 1e5 + 5; const int M = 30; const long long oo = LLONG_MAX; const int mod = 1e9 + 7; int f[N]; set<int> vec[N]; map<int, int> haveColor[N]; int find(int x) { if (f[x] < 0) return x; return f[x] = find(f[x]); } void match(int a, int b) { a = find(a); b = find(b); if (a == b) return; if (f[a] > f[b]) swap(a, b); f[a] += f[b]; f[b] = a; for (int x : vec[b]) { vec[a].insert(x); } } void add(int x, int y, int z) { vec[find(x)].insert(y); vec[find(y)].insert(x); if (haveColor[x].find(z) == haveColor[x].end()) haveColor[x][z] = y; else match(haveColor[x][z], y); if (haveColor[y].find(z) == haveColor[y].end()) haveColor[y][z] = x; else match(haveColor[y][z], x); } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, c, q; cin >> n >> m >> c >> q; for (int i = 0; i < N; ++i) f[i] = -1; for (int i = 0; i < m; ++i) { int x, y, z; cin >> x >> y >> z; add(x, y, z); } while (q--) { char opt; cin >> opt; if (opt == '+') { int x, y, z; cin >> x >> y >> z; add(x, y, z); } else { int x, y; cin >> x >> y; if (vec[find(x)].count(y) || find(x) == find(y)) { cout << "Yes" << '\n'; } else { cout << "No" << '\n'; } } } return 0; }
#include <bits/stdc++.h> using namespace std; ifstream in; ofstream out; const long long kk = 1000; const long long ml = kk * kk; const long long mod = ml * kk + 7; const long long inf = ml * ml * ml + 7; const long long gs = 500 * kk; long n, m, i, j, c, q; vector<long> reb[gs + 10]; vector<unordered_map<long long, long long>> have; vector<set<long long>> nb; vector<long long> col, tc, par, sz; bool viv = false; void prepare() { for (long long i = 0; i < n; i++) par.push_back(i); sz.resize(n, 1); nb.resize(n); have.resize(n); } long long lead(long long v) { if (par[v] == v) return v; par[v] = lead(par[v]); return par[v]; } void merge(long long v1, long long v2) { if (v1 == v2) return; if (sz[v1] < sz[v2]) swap(v1, v2); sz[v1] += sz[v2]; sz[v2] = 0; par[v2] = v1; for (auto v : nb[v2]) nb[v1].insert(v); } void add(long long v1, long long v2, long long cr) { if (have[v1].find(cr) != have[v1].end()) merge(lead(v2), lead(have[v1][cr])); if (have[v2].find(cr) != have[v2].end()) merge(lead(v1), lead(have[v2][cr])); nb[lead(v1)].insert(v2); nb[lead(v2)].insert(v1); have[v1][cr] = v2; have[v2][cr] = v1; } bool exist(long long st, long long fin) { if (lead(st) == lead(fin)) return true; long long v = lead(st); if (nb[v].find(fin) != nb[v].end()) return true; return false; } void qrs() { for (long long i = 0; i < q; i++) { string str; cin >> str; if (str == "+") { long long a, b, c; cin >> a >> b >> c; a--, b--, c--; add(a, b, c); } else { long long a, b; cin >> a >> b; a--, b--; if (exist(a, b)) cout << "Yes\n"; else cout << "No\n"; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> c >> q; prepare(); for (i = 0; i < m; i++) { long a, b, c; cin >> a >> b >> c; a--; b--; c--; add(a, b, c); } qrs(); return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxn = 1e5 + 5; int d[maxn]; unordered_map<int, int> mp[maxn]; unordered_set<int> st[maxn]; int fi(int x) { return d[x] == x ? x : d[x] = fi(d[x]); } void merge(int x, int y) { if (st[x].size() > st[y].size()) { swap(x, y); } d[x] = y; for (auto &i : st[x]) { st[y].insert(i); } } void add_edge(int x, int y, int c) { st[fi(x)].insert(y); st[fi(y)].insert(x); if (mp[x].count(c)) { int m = mp[x][c]; if (fi(m) != fi(y)) { merge(fi(m), fi(y)); } } if (mp[y].count(c)) { int m = mp[y][c]; if (fi(m) != fi(x)) { merge(fi(m), fi(x)); } } mp[x][c] = y; mp[y][c] = x; } void solve() { int n, m, c, q; cin >> n >> m >> c >> q; for (int i = 1; i < n + 1; i++) { d[i] = i; } for (int i = 0; i < m; i++) { int x, y, cc; cin >> x >> y >> cc; add_edge(x, y, cc); } for (int i = 0; i < q; i++) { char c; cin >> c; if (c == '?') { int x, y; cin >> x >> y; if (fi(x) == fi(y)) { cout << "Yes\n"; } else if (st[fi(x)].count(y)) { cout << "Yes\n"; } else { cout << "No\n"; } } else { int x, y, c; cin >> x >> y >> c; add_edge(x, y, c); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int c = 0; int t; if (!c) { t = 1; } else { cin >> t; } while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; unordered_map<int, int> col[200010]; set<int> S[200010]; int fa[200010]; inline int Find(int x) { return fa[x] == x ? x : fa[x] = Find(fa[x]); } inline void merge(int A, int B) { if (A == B) return; if (S[A].size() > S[B].size()) swap(A, B); for (set<int>::iterator it = S[A].begin(); it != S[A].end(); ++it) { S[B].insert(*it); } S[A].clear(); fa[A] = B; } inline void add(int x, int y, int z) { if (col[x].count(z)) { merge(Find(col[x][z]), Find(y)); } if (col[y].count(z)) { merge(Find(col[y][z]), Find(x)); } col[x][z] = y; col[y][z] = x; S[Find(x)].insert(y); S[Find(y)].insert(x); } inline void query(int x, int y) { x = Find(x); int Y = Find(y); if (x == Y || S[x].count(y)) { puts("Yes"); } else puts("No"); } int main() { int n, m, c, q; scanf("%d%d%d%d", &n, &m, &c, &q); for (int i = 1; i <= n; ++i) { fa[i] = i; } for (int i = 1; i <= m; ++i) { int x, y, z; scanf("%d%d%d", &x, &y, &z); add(x, y, z); } while (q--) { char s[20]; int x, y, z; scanf("%s%d%d", s, &x, &y); if (s[0] == '+') { scanf("%d", &z); add(x, y, z); } else { query(x, y); } } }
#include <bits/stdc++.h> using namespace std; long n, m, c, q, u, v, par[100005]; char type; set<long> s[100005]; map<long, long> color[100005]; long find(int i) { if (par[i] == i) { return i; } else { return par[i] = find(par[i]); } } void uni(int u, int v) { long par_u = find(u), par_v = find(v); if (par_u == par_v) { return; } if (s[par_u].size() > s[par_v].size()) { swap(s[par_u], s[par_v]); } for (auto w : s[par_u]) { s[par_v].insert(w); } par[par_u] = par_v; } void add(int u, int v, int c) { if (color[u][c] != 0) { uni(v, color[u][c]); } if (color[v][c] != 0) { uni(u, color[v][c]); } s[find(u)].insert(v); s[find(v)].insert(u); color[u][c] = v; color[v][c] = u; } int main() { scanf("%ld %ld %ld %ld", &n, &m, &c, &q); for (int i = 1; i <= n; i++) { par[i] = i; } for (int i = 1; i <= m; i++) { scanf("%ld %ld %ld", &u, &v, &c); add(u, v, c); } for (int i = 1; i <= q; i++) { cin >> type; if (type == '+') { scanf("%ld %ld %ld", &u, &v, &c); add(u, v, c); } else { scanf("%ld %ld", &u, &v); long par_u = find(u), par_v = find(v); if (par_u == par_v || s[par_u].find(v) != s[par_u].end()) { printf("Yes\n"); } else { printf("No\n"); } } } }
#include <bits/stdc++.h> using namespace std; template <class T> inline void rd(T &x) { x = 0; char c = getchar(); int f = 1; while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) x = x * 10 - '0' + c, c = getchar(); x *= f; } const int N = 1e5 + 10; map<int, int> col[N]; set<int> S[N], nxt[N]; int fa[N], n; int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } void un(int x, int y) { int fx = find(x), fy = find(y); if (fx == fy) return; if (S[fx].size() > S[fy].size()) swap(fx, fy); for (set<int>::iterator it = S[fx].begin(); it != S[fx].end(); ++it) { nxt[*it].erase(fx), nxt[*it].insert(fy); S[fy].insert(*it); } fa[fx] = fy; } void adde(int x, int y, int w) { S[find(x)].insert(y), nxt[y].insert(find(x)); S[find(y)].insert(x), nxt[x].insert(find(y)); if (!col[x].count(w)) col[x][w] = y; else un(y, col[x][w]); if (!col[y].count(w)) col[y][w] = x; else un(x, col[y][w]); } int main() { int x, y, w, m, q; rd(n), rd(m), rd(x), rd(q); for (int i = 1; i <= n; ++i) fa[i] = i; for (int i = 1; i <= m; ++i) rd(x), rd(y), rd(w), adde(x, y, w); char op[10]; while (q--) { scanf("%s", op); if (op[0] == '+') rd(x), rd(y), rd(w), adde(x, y, w); else { rd(x), rd(y); if (find(x) == find(y) || nxt[y].count(find(x))) printf("Yes\n"); else printf("No\n"); } } return 0; }
#include <bits/stdc++.h> const int N = 100054; int V, E, q; int p[N]; std::set<int> adj[N]; std::map<int, int> ep[N]; int ancestor(int x) { return p[x] == x ? x : (p[x] = ancestor(p[x])); } inline bool test(int x, int y, bool un = false) { if ((x = ancestor(x)) == (y = ancestor(y))) return true; return un && (p[x] = y, false); } void merge(std::set<int> &dest, std::set<int> &src) { if (dest.size() < src.size()) dest.swap(src); for (int x : src) dest.emplace(x); src.clear(); } void link(int u, int v, int w) { int x; std::map<int, int>::iterator it; bool ret; adj[ancestor(u)].emplace(v), std::tie(it, ret) = ep[u].emplace(w, v); if (!(ret || test(x = ancestor(it->second), v = ancestor(v), true))) merge(adj[v], adj[x]); } int main() { int i, u, v, w; char op[4]; scanf("%d%d%*d%d", &V, &E, &q); std::iota(p, p + (V + 1), 0); for (i = 0; i < E; ++i) scanf("%d%d%d", &u, &v, &w), link(u, v, w), link(v, u, w); for (; q; --q) if (scanf("%s", op), *op == 43) scanf("%d%d%d", &u, &v, &w), link(u, v, w), link(v, u, w); else scanf("%d%d", &u, &v), puts(test(u, v) || adj[ancestor(u)].count(v) ? "Yes" : "No"); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; map<int, int> Q[N]; int dsu[N]; set<int> nearest[N]; int n, m, C, q; int get(int v) { if (dsu[v] == v) { return v; } return dsu[v] = get(dsu[v]); } bool does(int a, int b) { if (get(a) == get(b)) { return 1; } return nearest[get(a)].count(b); } void merge(int a, int b) { a = get(a); b = get(b); if (a == b) { return; } if (nearest[a].size() < nearest[b].size()) { swap(a, b); } for (auto i : nearest[b]) { nearest[a].insert(i); } dsu[b] = a; } void addEdge(int a, int b, int c) { if (Q[a].count(c)) { merge(b, Q[a][c]); } if (Q[b].count(c)) { merge(a, Q[b][c]); } Q[a][c] = b; Q[b][c] = a; nearest[get(a)].insert(b); nearest[get(b)].insert(a); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> C >> q; for (int i = 0; i < n; ++i) { dsu[i] = i; } for (int i = 0; i < m; ++i) { int a, b, c; cin >> a >> b >> c; a--; b--; addEdge(a, b, c); } for (int i = 0; i < q; ++i) { char c; cin >> c; if (c == '+') { int a, b, c; cin >> a >> b >> c; a--; b--; addEdge(a, b, c); } else { int a, b; cin >> a >> b; a--; b--; if (does(a, b)) { cout << "Yes\n"; } else { cout << "No\n"; } } } return 0; }
#include <bits/stdc++.h> using namespace std; int ds[100100]; set<int> neighbors[100100]; map<pair<int, int>, int> coloredNode; int get(int x) { return ds[x] == x ? x : ds[x] = get(ds[x]); } void join(int x, int y) { int dx = get(x), dy = get(y); if (dx != dy) { if (neighbors[dx].size() < neighbors[dy].size()) swap(dx, dy); for (int u : neighbors[dy]) neighbors[dx].insert(u); ds[dy] = dx; } } void addEdge(int x, int y, int z) { neighbors[get(x)].insert(y); if (!coloredNode.count({y, z})) coloredNode[{y, z}] = x; else join(coloredNode[{y, z}], x); } string query(int x, int y) { int dx = get(x), dy = get(y); return dx == dy || neighbors[dx].count(y) ? "Yes" : "No"; } int main() { ios::sync_with_stdio(0); int n, m, c, q, x, y, z; cin >> n >> m >> c >> q; for (int i = 1; i <= n; i++) { ds[i] = i; neighbors[i].insert(i); } while (m--) { cin >> x >> y >> z; addEdge(x, y, z); addEdge(y, x, z); } while (q--) { char t; cin >> t >> x >> y; if (t == '?') cout << query(x, y) << '\n'; else { cin >> z; addEdge(x, y, z); addEdge(y, x, z); } } }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); size_t n, m, c, q; cin >> n >> m >> c >> q; vector<map<size_t, size_t>> adj(++n); vector<size_t> fa(n); iota(fa.begin(), fa.end(), 0); vector<set<size_t>> last(n); for (size_t i = 0; i != n; ++i) last[i].emplace(i); auto find = [&](const size_t& x) { size_t f = -1; vector<size_t> stack(1, x); while (!stack.empty()) { if (fa[stack.back()] == stack.back()) { f = stack.back(); stack.pop_back(); } else { if (~f) { fa[stack.back()] = f; stack.pop_back(); } else { stack.emplace_back(fa[stack.back()]); } } } return f; }; auto merge = [&](size_t x, size_t y) { x = find(x); y = find(y); if (x != y) { if (last[x].size() < last[y].size()) swap(x, y); last[x].insert(last[y].begin(), last[y].end()); last[y].clear(); fa[y] = x; } }; auto link = [&]() { size_t x, y, z; cin >> x >> y >> z; auto ite = adj[x].find(z); if (ite != adj[x].end()) merge(y, ite->second); ite = adj[y].find(z); if (ite != adj[y].end()) merge(x, ite->second); adj[x][z] = y; adj[y][z] = x; last[find(x)].emplace(y); last[find(y)].emplace(x); }; auto query = [&]() { size_t x, y; cin >> x >> y; x = find(x); cout << (last[x].find(y) != last[x].end() ? "Yes" : "No") << '\n'; }; while (m--) link(); while (q--) { char op; cin >> op; if (op == '+') link(); else query(); } }
#include <bits/stdc++.h> using namespace std; const long long SZ = 1e6 + 100, INF = 1e9 * 1e9 + 100, MOD = 1e9 + 7, K = 510; long long n, m, c, q; map<pair<long long, long long>, long long> ind; long long par[SZ]; set<long long> st[SZ]; long long find(long long v) { if (par[v] == v) return v; else return par[v] = find(par[v]); } void uni(int v, int u) { v = find(v); u = find(u); if (v != u) { if (st[v].size() > st[u].size()) { swap(v, u); } par[v] = u; for (auto c : st[v]) { st[u].insert(c); } st[v].clear(); } } void create(int v, int c) { if (ind.find({v, c}) != ind.end()) return; long long k = ind.size(); ind[{v, c}] = k; par[k] = k; if (c == 0) { st[k].insert(v); } } void add(int v, int u, int c) { create(v, c); create(u, c); uni(ind[{v, c}], ind[{u, 0}]); uni(ind[{u, c}], ind[{v, 0}]); st[find(v)].insert(u); st[find(u)].insert(v); } bool ask(int v, int u) { v = find(v); if (v == find(u) || st[v].find(u) != st[v].end()) { return true; } return false; } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); ; cin >> n >> m >> c >> q; for (int i = 0; i < n; i++) { create(i, 0); } for (int i = 0; i < m; i++) { long long u, v, c; cin >> u >> v >> c; u--; v--; add(u, v, c); } while (q--) { char t; cin >> t; if (t == '+') { long long u, v, c; cin >> u >> v >> c; u--; v--; add(v, u, c); } else { long long u, v; cin >> u >> v; u--; v--; if (ask(u, v)) { cout << "Yes\n"; } else { cout << "No\n"; } } } return 0; }
#include <bits/stdc++.h> using namespace std; long long MOD = 1000000007; struct DSU { vector<int> rep; vector<set<int>> edges; void clear() { iota((rep).begin(), (rep).end(), 0); } DSU(int n = 0) : rep(n + 1), edges(n + 1) { clear(); } int find(int a) { return rep[a] == a ? a : rep[a] = find(rep[a]); } void merge(int a, int b) { if (a == 0 or b == 0) return; if (edges[a = find(a)].size() < edges[b = find(b)].size()) swap(a, b); if (a == b) return; for (auto x : edges[b]) edges[a].insert(x); rep[b] = a; } void add_edge(int a, int b) { edges[find(a)].insert(b), edges[find(b)].insert(a); } }; const long long LIM = 1e5 + 5; map<long long, long long> mmap[LIM]; void solve() { long long n, m, c, q; cin >> n >> m >> c >> q; DSU D = DSU(n); for (long long i = 0; i < m; i++) { long long x, y, z; cin >> x >> y >> z; D.merge(mmap[x][z], y); D.merge(mmap[y][z], x); mmap[x][z] = y; mmap[y][z] = x; D.add_edge(x, y); } for (long long i = 0; i < q; i++) { char ch; cin >> ch; if (ch == '?') { long long x, y; cin >> x >> y; long long rx = D.find(x); long long ry = D.find(y); bool bo = (rx == ry || D.edges[rx].find(y) != D.edges[rx].end()); if (bo) cout << "Yes" << "\n"; else cout << "No" << "\n"; } else { long long x, y, z; cin >> x >> y >> z; D.merge(mmap[x][z], y); D.merge(mmap[y][z], x); mmap[x][z] = y; mmap[y][z] = x; D.add_edge(x, y); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; for (long long i = 0; i < t; i++) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const int mod = 1000000007; const vector<int> mods = {998244353, 1000000007, 1000000009}; const long long inf = 3e18; const double eps = 0.0000001; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); long long n, m, c, q, x, y, z; string s; unordered_map<long long, long long> cg; unordered_map<long long, unordered_set<long long>> g; long long par[100100], siz[100100]; long long fnd(long long n1) { if (par[n1] != n1) par[n1] = fnd(par[n1]); return par[n1]; } void unn(long long n1, long long n2) { long long p1 = fnd(n1), p2 = fnd(n2); if (p1 == p2) return; if (siz[p1] < siz[p2]) swap(p1, p2); par[p2] = p1; siz[p1] += siz[p2]; for (auto& nx : g[p2]) g[p1].insert(nx); } void add() { cin >> x >> y >> z; x--; y--; if ((cg.find(x * 100100 + z) != end(cg))) unn(fnd(y), fnd(cg[x * 100100 + z])); else cg[x * 100100 + z] = fnd(y); if ((cg.find(y * 100100 + z) != end(cg))) unn(fnd(x), fnd(cg[y * 100100 + z])); else cg[y * 100100 + z] = fnd(x); g[fnd(x)].insert(y); g[fnd(y)].insert(x); } int main() { cin.sync_with_stdio(0); cin.tie(0); cout.precision(13); cin >> n >> m >> c >> q; for (long long i = (0); i < (n); ++i) { par[i] = i; siz[i] = 1; g[i].insert(i); } for (long long i = (0); i < (m); ++i) add(); for (long long _q = (0); _q < (q); ++_q) { cin >> s; if (s[0] == '?') { cin >> x >> y; x--; y--; if ((g[fnd(x)].find(y) != end(g[fnd(x)]))) cout << "Yes\n"; else cout << "No\n"; } else add(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, m, c, q, u, v, cnt = 0; char t; map<pair<int, int>, int> ind; vector<int> dsu; vector<set<int>> odd; int get(int u, int c) { if (ind.count({u, c}) == 0) { ind[{u, c}] = cnt++; dsu.push_back(-1); odd.push_back({}); } return ind[{u, c}]; } int trace(int u) { return dsu[u] < 0 ? u : dsu[u] = trace(dsu[u]); } void connect(int u, int v) { if ((u = trace(u)) == (v = trace(v))) return; if (dsu[u] > dsu[v]) swap(u, v); dsu[u] += dsu[v]; dsu[v] = u; if (odd[u].size() < odd[v].size()) swap(odd[u], odd[v]); for (int ele : odd[v]) odd[u].insert(ele); odd[v].clear(); } void add(int u, int v) { u = trace(u); odd[u].insert(v); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m >> c >> q; for (int i = 1; i <= n; i++) get(i, 0); while (m--) { cin >> u >> v >> c; int l = get(u, c), r = get(v, c); add(l, u); add(r, v); connect(l, get(v, 0)); connect(r, get(u, 0)); } while (q--) { cin >> t >> u >> v; if (t == '?') { if (trace(get(u, 0)) == trace(get(v, 0))) cout << "Yes\n"; else if (odd[trace(get(u, 0))].count(v) == 1) cout << "Yes\n"; else cout << "No\n"; } else { cin >> c; int l = get(u, c), r = get(v, c); add(l, u); add(r, v); connect(l, get(v, 0)); connect(r, get(u, 0)); } } }
#include <bits/stdc++.h> long long dx[8] = {0, 1, 0, -1, 1, 1, -1, -1}; long long dy[8] = {1, 0, -1, 0, -1, 1, 1, -1}; using namespace std; class pa3 { public: long long x; long long y, z; pa3(long long x = 0, long long y = 0, long long z = 0) : x(x), y(y), z(z) {} bool operator<(const pa3 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; return z < p.z; } bool operator>(const pa3 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; return z > p.z; } bool operator==(const pa3 &p) const { return x == p.x && y == p.y && z == p.z; } bool operator!=(const pa3 &p) const { return !(x == p.x && y == p.y && z == p.z); } }; class pa4 { public: long long x; long long y, z, w; pa4(long long x = 0, long long y = 0, long long z = 0, long long w = 0) : x(x), y(y), z(z), w(w) {} bool operator<(const pa4 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; if (z != p.z) return z < p.z; return w < p.w; } bool operator>(const pa4 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; if (z != p.z) return z > p.z; return w > p.w; } bool operator==(const pa4 &p) const { return x == p.x && y == p.y && z == p.z && w == p.w; } }; class pa2 { public: long long x, y; pa2(long long x = 0, long long y = 0) : x(x), y(y) {} pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); } pa2 operator-(pa2 p) { return pa2(x - p.x, y - p.y); } bool operator<(const pa2 &p) const { return y != p.y ? y < p.y : x < p.x; } bool operator>(const pa2 &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const pa2 &p) const { return abs(x - p.x) == 0 && abs(y - p.y) == 0; } bool operator!=(const pa2 &p) const { return !(abs(x - p.x) == 0 && abs(y - p.y) == 0); } }; string itos(long long i) { ostringstream s; s << i; return s.str(); } long long gcd(long long v, long long b) { if (v > b) return gcd(b, v); if (v == b) return b; if (b % v == 0) return v; return gcd(v, b % v); } long long mod; long long extgcd(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return a; } long long d = extgcd(b, a % b, y, x); y -= a / b * x; return d; } pair<long long, long long> operator+(const pair<long long, long long> &l, const pair<long long, long long> &r) { return {l.first + r.first, l.second + r.second}; } pair<long long, long long> operator-(const pair<long long, long long> &l, const pair<long long, long long> &r) { return {l.first - r.first, l.second - r.second}; } long long pr[200010]; long long inv[200010]; long long beki(long long wa, long long rr, long long warukazu) { if (rr == 0) return 1 % warukazu; if (rr == 1) return wa % warukazu; wa %= warukazu; if (rr % 2 == 1) return ((long long)beki(wa, rr - 1, warukazu) * (long long)wa) % warukazu; long long zx = beki(wa, rr / 2, warukazu); return (zx * zx) % warukazu; } long long comb(long long nn, long long rr) { if (rr < 0 || rr > nn || nn < 0) return 0; long long r = pr[nn] * inv[rr]; r %= mod; r *= inv[nn - rr]; r %= mod; return r; } void gya(long long ert) { pr[0] = 1; for (long long i = 1; i <= ert; i++) { pr[i] = (pr[i - 1] * i) % mod; } inv[ert] = beki(pr[ert], mod - 2, mod); for (long long i = ert - 1; i >= 0; i--) { inv[i] = inv[i + 1] * (i + 1) % mod; } } set<long long> se[100020]; struct unionfind { private: public: vector<long long> par, ranks, kosuu; void shoki(long long N) { par.resize(N + 1, 0); ranks.resize(N + 1, 0); kosuu.resize(N + 1, 1); for (long long i = 0; i <= N; i++) { par[i] = i; } } long long root(long long x) { return par[x] == x ? x : par[x] = root(par[x]); } bool same(long long x, long long y) { return root(x) == root(y); } bool is_root(long long x) { return x == root(x); } void unite(long long x, long long y) { x = root(x); y = root(y); long long xx = kosuu[x], yy = kosuu[y]; if (x == y) return; long long oya; if (se[x].size() < se[y].size()) { for (auto it = se[x].begin(); it != se[x].end(); it++) { se[y].insert(*it); } se[x].clear(); oya = y; } else { for (auto it = se[y].begin(); it != se[y].end(); it++) { se[x].insert(*it); } se[y].clear(); oya = x; } if (ranks[x] < ranks[y]) { par[x] = y; kosuu[y] = yy + xx; if (oya == x) { swap(se[x], se[y]); } } else { par[y] = x; if (ranks[x] == ranks[y]) ranks[x] = ranks[x] + 1; kosuu[x] = yy + xx; if (oya == y) { swap(se[x], se[y]); } } return; } }; vector<pair<long long, long long> > G[100020]; vector<pa4> que; map<long long, long long> ma[100020]; signed main() { cin.tie(0); ios::sync_with_stdio(false); long long n, m, c, q; cin >> n >> m >> c >> q; unionfind uf; uf.shoki(n + 5); for (long long i = 0; i < m; i++) { long long y, yy, yyy; cin >> y >> yy >> yyy; que.push_back({0, y, yy, yyy}); } for (long long i = 0; i < q; i++) { string s; cin >> s; if (s == "+") { long long y, yy, yyy; cin >> y >> yy >> yyy; que.push_back({0, y, yy, yyy}); } else { long long y, yy; cin >> y >> yy; que.push_back({1, y, yy, 0}); } } for (auto V : que) { if (V.x) { long long s = V.y, t = V.z; if (uf.same(s, t)) { cout << "Yes" << endl; continue; } long long ss = uf.root(s); if (se[ss].count(t)) { cout << "Yes" << endl; } else cout << "No" << endl; } else { long long s = V.y, t = V.z, c = V.w; if (ma[s].count(c)) { uf.unite(t, ma[s][c]); } if (ma[t].count(c)) { uf.unite(s, ma[t][c]); } ma[s][c] = t; ma[t][c] = s; long long ss = uf.root(s); long long tt = uf.root(t); se[ss].insert(t); se[tt].insert(s); } } return 0; }
#include <bits/stdc++.h> using namespace std; int i, i0, n, m, c, q, pre[100005]; map<int, vector<int>> mp[100005]; set<int> dic[100005]; int fin(int x) { return (pre[x] == x) ? x : pre[x] = fin(pre[x]); } void uni(int x, int y) { if (fin(x) != fin(y)) { if (dic[fin(x)].size() > dic[fin(y)].size()) for (auto i : dic[fin(y)]) dic[fin(x)].insert(i), pre[fin(y)] = fin(x); else for (auto i : dic[fin(x)]) dic[fin(y)].insert(i), pre[fin(x)] = fin(y); } } int main() { scanf("%d %d %d %d", &n, &m, &c, &q); for (i = 1; i <= n; i++) pre[i] = i; while (m--) { int x, y, col; scanf("%d %d %d", &x, &y, &col); dic[fin(x)].insert(y), dic[fin(y)].insert(x); mp[x][col].push_back(y), uni(y, mp[x][col][0]); mp[y][col].push_back(x), uni(x, mp[y][col][0]); } while (q--) { char op; scanf(" %c", &op); if (op == '?') { int x, y; scanf("%d %d", &x, &y); if (fin(x) == fin(y) || dic[fin(x)].count(y)) printf("Yes\n"); else printf("No\n"); } else { int x, y, col; scanf("%d %d %d", &x, &y, &col); dic[fin(x)].insert(y), dic[fin(y)].insert(x); mp[x][col].push_back(y), uni(y, mp[x][col][0]); mp[y][col].push_back(x), uni(x, mp[y][col][0]); } } return 0; }