This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define maxn 300050
int n,m,q;
vector<int> ufds(maxn);
vector<vector<int> > nodes(maxn);
vector<int> ins(maxn,0);
vector<int> depth(maxn,-1);
vector<vector<int> > adj(maxn);
vector<int> treated(maxn,0);
vector<int> ans(maxn,0);
vector<vector<int> > gives(maxn);
int find(int k){
if(ufds[k]!=k){
ufds[k] = find(ufds[k]);
}
return ufds[k];
}
void merge(int a,int b){
int temp1 = find(a);
int temp2 = find(b);
if(temp1==temp2){
return;
}
if(nodes[temp1].size()<nodes[temp2].size()){
swap(temp1,temp2);
}
for(auto k:nodes[temp2]){
nodes[temp1].push_back(k);
}
nodes[temp2].clear();
ufds[temp2] = temp1;
}
int find_height(int node){
if(depth[node]!=-1){
return depth[node];
}
int mxx = 0;
for(auto k:adj[node]){
int aa = find(k);
int tt = find_height(aa);
if(tt>mxx){
gives[node].clear();
gives[node].push_back(aa);
mxx = tt;
}
else if(tt==mxx){
gives[node].push_back(aa);
}
}
//cout << node << " " << mxx+1 << "--\n";
depth[node] = mxx+1;
return depth[node];
}
void update(int node,int current){
if(ans[node]!=0){
return;
}
ans[node] = current;
for(auto k:gives[node]){
update(k,current-1);
}
return;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n >> m >> q;
for(int i=0;i<ufds.size();i++){
ufds[i] = i;
nodes[i].push_back(i);
}
int a,c;
char b;
for(int i=0;i<q;i++){
cin >> a >> b >> c;
if(b=='<'){
adj[c].push_back(a);
ins[a]++;
}
else if(b=='='){
merge(a,c);
}
}
for(int i=1;i<=m;i++){
int tt = find(i);
if(tt==i){
continue;
}
for(auto k:adj[i]){
adj[tt].push_back(k);
}
}
for(int i=1;i<=m;i++){
if(ins[i]==0){
int aa = find_height(i);
//cout << i << " " << aa << endl;
if(aa==n){
update(i,n);
}
}
}
for(int i=1;i<=m;i++){
if(ans[i]==0){
continue;
}
int par = find(i);
if(treated[par]==1){
continue;
}
treated[par] = 1;
for(auto k:nodes[par]){
ans[k] = ans[i];
}
}
for(int i=1;i<=m;i++){
if(ans[i]==0){
cout << "?" << "\n";
}
else{
cout << "K" << ans[i] << "\n";
}
}
}
Compilation message (stderr)
kovanice.cpp: In function 'int32_t main()':
kovanice.cpp:70:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int i=0;i<ufds.size();i++){
| ~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |