Submission #680239

# Submission time Handle Problem Language Result Execution time Memory
680239 2023-01-10T10:16:12 Z raysh07 Experimental Charges (NOI19_charges) C++17
100 / 100
29 ms 4088 KB
#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 time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 3408 KB Output is correct
2 Correct 21 ms 3440 KB Output is correct
3 Correct 19 ms 3492 KB Output is correct
4 Correct 23 ms 3532 KB Output is correct
5 Correct 19 ms 3456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 4064 KB Output is correct
2 Correct 19 ms 3916 KB Output is correct
3 Correct 21 ms 3924 KB Output is correct
4 Correct 22 ms 3988 KB Output is correct
5 Correct 22 ms 4088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 3844 KB Output is correct
2 Correct 21 ms 3764 KB Output is correct
3 Correct 21 ms 3728 KB Output is correct
4 Correct 22 ms 4012 KB Output is correct
5 Correct 23 ms 3908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 18 ms 3408 KB Output is correct
7 Correct 21 ms 3440 KB Output is correct
8 Correct 19 ms 3492 KB Output is correct
9 Correct 23 ms 3532 KB Output is correct
10 Correct 19 ms 3456 KB Output is correct
11 Correct 20 ms 4064 KB Output is correct
12 Correct 19 ms 3916 KB Output is correct
13 Correct 21 ms 3924 KB Output is correct
14 Correct 22 ms 3988 KB Output is correct
15 Correct 22 ms 4088 KB Output is correct
16 Correct 20 ms 3844 KB Output is correct
17 Correct 21 ms 3764 KB Output is correct
18 Correct 21 ms 3728 KB Output is correct
19 Correct 22 ms 4012 KB Output is correct
20 Correct 23 ms 3908 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 29 ms 3884 KB Output is correct
27 Correct 21 ms 3908 KB Output is correct
28 Correct 20 ms 3792 KB Output is correct
29 Correct 23 ms 3748 KB Output is correct
30 Correct 20 ms 3804 KB Output is correct