Submission #425460

# Submission time Handle Problem Language Result Execution time Memory
425460 2021-06-13T04:04:04 Z errorgorn Experimental Charges (NOI19_charges) C++17
100 / 100
41 ms 9744 KB
#include <cstdio>
#include <vector>
#include <utility>
using namespace std;
typedef pair<int,int> ii;
typedef pair<ii, int> iii;
vector<ii> al[100005];
vector<iii> s;
int charge[100005],p[100005],r[100005];
inline int read() {
    int x = 0;
    char ch = getchar_unlocked();
    while (ch >= '0' && ch <= '9'){
		x = (x << 3) + (x << 1) + ch - '0';
		ch = getchar();
	  }
    return x;
}
void dfs(int i){
  for (vector<ii>::iterator it=al[i].begin();it!=al[i].end();it++){
    if (charge[(*it).first]==-1){
      charge[(*it).first]=charge[i]^(*it).second;
      dfs((*it).first);
    }
  }
}
int parent(int i){
  return (p[i]==i ? i : p[i]=parent(p[i]));
}
bool same(int i,int j){
  return parent(i)==parent(j);
}
void unions(int i,int j){
  i=parent(i),j=parent(j);
  if (i==j) return;
  if (r[i]<r[j]) p[i]=j;
  else{
    p[j]=i;
    if (r[i]==r[j]) r[i]++;
  }
}
int main(){
  for (int x=0;x<100005;x++){
    p[x]=x;
    charge[x]=-1;
  }
  int n,q,a,b;
  char c;
  n=read(),q=read();
  while (q--){
    c=getchar();
    getchar();
    a=read(),b=read();
    if (c=='Q') s.push_back(iii (ii(a,b),0));
    else{
      s.push_back(iii (ii(a,b),1));
      al[a].push_back(ii (b,(c=='R')? 0:1));
      al[b].push_back(ii (a,(c=='R')? 0:1));
    }
  }
  for (int x=1;x<=n;x++){
    if (charge[x]==-1){
      charge[x]=0;
      dfs(x);
    }
  }
  for (vector<iii>::iterator it=s.begin();it!=s.end();it++){
    a=(*it).first.first,b=(*it).first.second,c=(*it).second;
    if ((*it).second==0){
      putchar_unlocked(same(a,b) ? ((charge[a]==charge[b])? 'R':'A'): '?');
      putchar_unlocked('\n');
    }
    else unions(a,b);
  }
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3428 KB Output is correct
2 Correct 2 ms 3404 KB Output is correct
3 Correct 2 ms 3404 KB Output is correct
4 Correct 3 ms 3404 KB Output is correct
5 Correct 2 ms 3404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 7820 KB Output is correct
2 Correct 20 ms 8052 KB Output is correct
3 Correct 20 ms 7084 KB Output is correct
4 Correct 19 ms 7672 KB Output is correct
5 Correct 25 ms 7804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 9744 KB Output is correct
2 Correct 27 ms 8776 KB Output is correct
3 Correct 24 ms 8056 KB Output is correct
4 Correct 34 ms 9100 KB Output is correct
5 Correct 37 ms 8640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 9628 KB Output is correct
2 Correct 40 ms 8852 KB Output is correct
3 Correct 37 ms 8572 KB Output is correct
4 Correct 34 ms 8984 KB Output is correct
5 Correct 35 ms 8992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3428 KB Output is correct
2 Correct 2 ms 3404 KB Output is correct
3 Correct 2 ms 3404 KB Output is correct
4 Correct 3 ms 3404 KB Output is correct
5 Correct 2 ms 3404 KB Output is correct
6 Correct 3 ms 3404 KB Output is correct
7 Correct 2 ms 3404 KB Output is correct
8 Correct 7 ms 3432 KB Output is correct
9 Correct 4 ms 3432 KB Output is correct
10 Correct 4 ms 3428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3428 KB Output is correct
2 Correct 2 ms 3404 KB Output is correct
3 Correct 2 ms 3404 KB Output is correct
4 Correct 3 ms 3404 KB Output is correct
5 Correct 2 ms 3404 KB Output is correct
6 Correct 22 ms 7820 KB Output is correct
7 Correct 20 ms 8052 KB Output is correct
8 Correct 20 ms 7084 KB Output is correct
9 Correct 19 ms 7672 KB Output is correct
10 Correct 25 ms 7804 KB Output is correct
11 Correct 27 ms 9744 KB Output is correct
12 Correct 27 ms 8776 KB Output is correct
13 Correct 24 ms 8056 KB Output is correct
14 Correct 34 ms 9100 KB Output is correct
15 Correct 37 ms 8640 KB Output is correct
16 Correct 18 ms 9628 KB Output is correct
17 Correct 40 ms 8852 KB Output is correct
18 Correct 37 ms 8572 KB Output is correct
19 Correct 34 ms 8984 KB Output is correct
20 Correct 35 ms 8992 KB Output is correct
21 Correct 3 ms 3404 KB Output is correct
22 Correct 2 ms 3404 KB Output is correct
23 Correct 7 ms 3432 KB Output is correct
24 Correct 4 ms 3432 KB Output is correct
25 Correct 4 ms 3428 KB Output is correct
26 Correct 20 ms 9648 KB Output is correct
27 Correct 27 ms 8652 KB Output is correct
28 Correct 31 ms 8824 KB Output is correct
29 Correct 31 ms 8752 KB Output is correct
30 Correct 41 ms 8756 KB Output is correct