답안 #1027379

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1027379 2024-07-19T05:29:16 Z caterpillow Experimental Charges (NOI19_charges) C++17
100 / 100
35 ms 8540 KB
#include <bits/stdc++.h>

using namespace std;

using db = long double;
using ll = long long;
using pl = pair<ll, ll>;
using pi = pair<int, int>;
#define vt vector
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end() 
#define size(x) ((int) (x).size())
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define F0R(i, b) FOR (i, 0, b)
#define endl '\n'
const ll INF = 1e18;
const int inf = 1e9;

template<typename Tuple, size_t... Is>
void print_tuple(const Tuple& t, index_sequence<Is...>) {
    ((cerr << (Is == 0 ? "{" : ", ") << get<Is>(t)), ...);
    cerr << "}";
}

template<typename... Args>
ostream& operator<<(ostream& os, const tuple<Args...>& t) {
    print_tuple(t, index_sequence_for<Args...>{});
    return os;
}

ostream& operator<<(ostream& os, string& s) {
    for (char c : s) os << c; 
    return os;
}

template<template<typename> class Container, typename T>
ostream& operator<<(ostream& os, Container<T> o) {
    os << "{"; 
    int g = size(o); 
    for (auto i : o) os << i << ((--g) == 0 ? "" : ", "); 
    os << "}";
    return os;
}

template<typename T, typename V>
ostream& operator<<(ostream& os, const pair<T, V> p) {
    os << "{" << p.f << ", " << p.s << "}";
    return os;
}

template <typename T, typename... V>
void printer(const char *names, T &&head, V &&...tail) {
    int i = 0;
    while (names[i] != '\0' && names[i] != ',') i++;
    constexpr bool is_str = is_same_v<decay_t<T>, const char*>;
    if constexpr (is_str) cerr << head; // ignore directly passed strings
    else cerr.write(names, i) << " = " << head; 
    if constexpr (sizeof...(tail)) cerr << (is_str ? "" : ","), printer(names + i + 1, tail...);
    else cerr << endl;
}

#ifdef LOCAL
#define dbg(...) std::cerr << __LINE__ << ": ", printer(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(x...)
#define cerr if (0) std::cerr
#endif

int n, q;
vt<vt<int>> buckets;
vt<int> rep;
vt<int> col;

void unite(int x, int y, int is_opposite) {
    if (rep[x] == rep[y]) {
        assert((col[x] ^ col[y]) == is_opposite);
        return;
    }
    bool should_flip = col[x] ^ col[y] ^ is_opposite;
    x = rep[x], y = rep[y];
    if (size(buckets[x]) > size(buckets[y])) swap(x, y);
    for (int e : buckets[x]) {
        rep[e] = y;
        buckets[y].pb(e);
        if (should_flip) col[e] ^= 1;
    }  
    buckets[x].clear();
}

main() {
    cin.tie(0)->sync_with_stdio(0);
    
    cin >> n >> q;
    buckets.resize(n);
    col = rep = vt<int>(n);
    iota(all(rep), 0);
    F0R (i, n) buckets[i].pb(i);

    F0R (i, q) {
        char t; cin >> t;
        int x, y; cin >> x >> y;
        x--, y--;
        if (t == 'Q') {
            if (rep[x] != rep[y]) cout << "?\n";
            else if (col[x] == col[y]) cout << "R\n";
            else cout << "A\n";
        } else if (t == 'A') {
            unite(x, y, 1);
        } else if (t == 'R') {
            unite(x, y, 0);
        } else assert(0);
    }

}

Compilation message

charges.cpp:93:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   93 | main() {
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 7636 KB Output is correct
2 Correct 22 ms 7636 KB Output is correct
3 Correct 21 ms 7512 KB Output is correct
4 Correct 22 ms 7636 KB Output is correct
5 Correct 20 ms 7664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 8404 KB Output is correct
2 Correct 21 ms 8148 KB Output is correct
3 Correct 26 ms 8020 KB Output is correct
4 Correct 30 ms 8396 KB Output is correct
5 Correct 26 ms 8540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 7876 KB Output is correct
2 Correct 21 ms 7896 KB Output is correct
3 Correct 35 ms 7888 KB Output is correct
4 Correct 25 ms 8272 KB Output is correct
5 Correct 26 ms 8144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 352 KB Output is correct
9 Correct 0 ms 416 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 25 ms 7636 KB Output is correct
7 Correct 22 ms 7636 KB Output is correct
8 Correct 21 ms 7512 KB Output is correct
9 Correct 22 ms 7636 KB Output is correct
10 Correct 20 ms 7664 KB Output is correct
11 Correct 20 ms 8404 KB Output is correct
12 Correct 21 ms 8148 KB Output is correct
13 Correct 26 ms 8020 KB Output is correct
14 Correct 30 ms 8396 KB Output is correct
15 Correct 26 ms 8540 KB Output is correct
16 Correct 19 ms 7876 KB Output is correct
17 Correct 21 ms 7896 KB Output is correct
18 Correct 35 ms 7888 KB Output is correct
19 Correct 25 ms 8272 KB Output is correct
20 Correct 26 ms 8144 KB Output is correct
21 Correct 1 ms 464 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 352 KB Output is correct
24 Correct 0 ms 416 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 20 ms 8228 KB Output is correct
27 Correct 25 ms 8152 KB Output is correct
28 Correct 23 ms 7928 KB Output is correct
29 Correct 28 ms 8140 KB Output is correct
30 Correct 24 ms 8152 KB Output is correct