#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
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++){
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
42744 KB |
Output is correct |
2 |
Correct |
38 ms |
42744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
157 ms |
47864 KB |
Output is correct |
2 |
Correct |
156 ms |
47992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
46072 KB |
Output is correct |
2 |
Correct |
59 ms |
46200 KB |
Output is correct |
3 |
Correct |
58 ms |
45560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
369 ms |
58876 KB |
Output is correct |
2 |
Correct |
354 ms |
58104 KB |
Output is correct |
3 |
Correct |
349 ms |
57976 KB |
Output is correct |
4 |
Correct |
326 ms |
59256 KB |
Output is correct |
5 |
Correct |
358 ms |
62968 KB |
Output is correct |