제출 #1276871

#제출 시각아이디문제언어결과실행 시간메모리
1276871vibeduckExperimental Charges (NOI19_charges)C++20
32 / 100
52 ms15796 KiB
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define int long long
#define all(x) (x).begin(), (x).end()

typedef long double ld;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ll>> vvll;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef map<int, int> mii;
typedef unordered_map<int, int> umii;

const int mod = 1e9 + 7;
const int inf = INTMAX_MAX;
const bool tc = false;

const int mxn = 1e5 + 1;
int c[mxn]; set<pii> adj[mxn];

class DisjointSets {
  private:
	vector<int> parents;
	vector<int> sizes;

  public:
	DisjointSets(int size) : parents(size), sizes(size, 1) {
		for (int i = 0; i < size; i++) { parents[i] = i; }
	}

	/** @return the "representative" node in x's component */
	int find(int x) { return parents[x] == x ? x : (parents[x] = find(parents[x])); }

	/** @return whether the merge changed connectivity */
	bool unite(int x, int y) {
		int x_root = find(x);
		int y_root = find(y);
		if (x_root == y_root) { return false; }

		if (sizes[x_root] < sizes[y_root]) { swap(x_root, y_root); }
		sizes[x_root] += sizes[y_root];
		parents[y_root] = x_root;
		return true;
	}

	/** @return whether x and y are in the same connected component */
	bool connected(int x, int y) { return find(x) == find(y); }
};

void dfs(int node, int col) {
    if (c[node] == col) return;
    c[node] = col;
    for (auto &neighbour : adj[node]) {
        if (c[neighbour.fi]) continue;
        dfs(neighbour.fi, col * neighbour.se);
    }
}

inline void solve() {
    int n, q;
    cin >> n >> q;
    DisjointSets dsu(n);

    while (q--) {
        char t; cin >> t;
        if (t == 'A') {
            // attract, different
            int x, y;
            cin >> x >> y;
            x--; y--;
            dsu.unite(x, y);
            adj[x].insert({y, -1});
            adj[y].insert({x, -1});
            if (!c[x] && !c[y]) {c[x] = 1; c[y] = -1; continue;}
            if (!c[x]) {
                // dfs from x
                dfs(x, -1 * c[y]);
            }
            if (!c[y]) {
                // dfs from y
                dfs(y, -1 * c[x]);
            }
            if (c[x] == c[y]) {
                dfs(x, -c[y]);
            }
            
        } else if (t == 'R') {
            int x, y;
            cin >> x >> y;
            x--; y--;
            dsu.unite(x, y);
            adj[x].insert({y, 1});
            adj[y].insert({x, 1});
            if (!c[x] && !c[y]) {c[x] = 1; c[y] = 1; continue;}
            if (!c[x]) {
                // dfs from x
                dfs(x, c[y]);
            }
            if (!c[y]) {
                // dfs from y
                dfs(y, c[x]);
            }
            if (c[x] != c[y]) {
                dfs(x, c[y]);
            }
            
        } else {
            // query
            int x, y;
            cin >> x >> y;
            x--; y--;
            if (!dsu.connected(x, y)) {
                cout << "?\n";
                continue;
            }
            cout << (c[x] == c[y] ? "R" : "A") << '\n';
        }
    }
}

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

signed main() {
    ios::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
    //setIO();

    int t = 1;
    if (tc) {
        cin >> t;
    }

    while (t--) {
        solve();
    }
}

컴파일 시 표준 에러 (stderr) 메시지

charges.cpp: In function 'void setIO(std::string)':
charges.cpp:137:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  137 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
charges.cpp:138:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  138 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...