Submission #680239

#TimeUsernameProblemLanguageResultExecution timeMemory
680239raysh07Experimental Charges (NOI19_charges)C++17
100 / 100
29 ms4088 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1e5 + 69;
int sz[maxn];
pair<int, int> root[maxn];

int findroot(int x)
{
    while (x!=root[x].first)
    x=root[x].first;
    return x;
}

int findvalroot(int x)
{
    int s = 0;
    while (x!=root[x].first)
    {
        s+= root[x].second;
        x = root[x].first;
    }
    return s;
}

void unite(int x, int y, int k)
{
    x = findroot(x);
    y = findroot(y);
    if (sz[y]>sz[x])
    swap(x, y);
    sz[x]+=sz[y];
    root[y] = make_pair(x, k);
}

void Solve() 
{
    int n, q;
    cin>>n>>q;
    for (int i=1; i<=n; i++)
    {
        sz[i] = 1;
        root[i] = make_pair(i, 0);
    }
    for (int i=0; i<q; i++)
    {
        char ch;
        int x, y;
        cin>>ch>>x>>y;
        if (ch=='R')
        {
            if (findroot(x)==findroot(y))
            continue;
            int v1 = findvalroot(x);
            int v2 = findvalroot(y);
            
            if ((v1+v2)%2==0)
            unite(x, y, 0);
            else unite(x, y, 1);
        }
        else if (ch=='A')
        {
            if (findroot(x)==findroot(y))
            continue;
            int v1 = findvalroot(x);
            int v2 = findvalroot(y);
            
            if ((v1+v2)%2==0)
            unite(x, y, 1);
            else unite(x, y, 0);
        }
        else 
        {
            if (findroot(x)!=findroot(y))
            cout<<"?\n";
            else if (findvalroot(x)%2==findvalroot(y)%2)
            cout<<"R\n";
            else cout<<"A\n";
        }
    }
}

int32_t main() 
{
    auto begin = std::chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    //cin >> t;
    for(int i = 1; i <= t; i++) 
    {
        //cout << "Case #" << i << ": ";
        Solve();
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
    return 0;
}
#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...