#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long long
struct DSU{
vector<int> p;
DSU(int n){
p.assign(n + 1, -1);
}
int find(int x){
if (p[x] < 0) return x;
return p[x] = find(p[x]);
}
void merge(int u, int v){
u = find(u);
v = find(v);
if (u == v) return;
if (p[u] > p[v]) swap(u, v);
p[u] += p[v];
p[v] = u;
}
};
vector<vector<int>> v, vr;
vector<int> ord, vis, lb, rb;
void dfs(int x){
vis[x] = 1;
ord.push_back(x);
for (auto el : v[x]){
if (vis[el] == 0){
dfs(el);
}
}
}
int main(){
string inp;
int n, m, c, a, b;
cin>>n>>m>>c;
v.assign(m + 1, vector<int>()), vr.assign(m + 1, vector<int>()), vis.assign(m + 1, 0), lb.assign(m + 1, 1), rb.assign(m + 1, n);
vector<vector<int>> edg;
DSU ds(m + 1);
for (int i = 0; i < c; i++){
string s1 = "", s2 = "";
cin>>inp;
int f = 0;
for (int i = 0; i < inp.size(); i++){
if (inp[i] == '='){
f = 2;
}
else if (inp[i] == '<'){
f = 1;
}
else if (inp[i] == '>'){
f = 3;
}
else{
if (f == 0){
s1 += inp[i];
}
else{
s2 += inp[i];
}
}
}
a = stoi(s1), b = stoi(s2);
if (f == 1){
edg.push_back({a, b});
}
else if (f == 2){
ds.merge(a, b);
}
else{
edg.push_back({b, a});
}
}
for (int i = 0; i < edg.size(); i++){
a = ds.find(edg[i][0]), b = ds.find(edg[i][1]);
v[a].push_back(b);
vr[b].push_back(a);
}
for (int i = 1; i <= m; i++){
if (vis[i] == 0){
dfs(i);
}
}
for (int i = 1; i <= m; i++){
for (auto el : vr[i]){
lb[i] = max(lb[i], lb[el] + 1);
}
}
for (int i = m; i >= 1; i--){
for (auto el : v[i]){
rb[i] = min(rb[i], rb[el] - 1);
}
}
for (int i = 1; i <= m; i++){
int a = ds.find(i);
if (lb[a] == rb[a]){
cout<<"K"<<lb[a]<<"\n";
}
else{
cout<<"?\n";
}
}
}
Compilation message
kovanice.cpp: In function 'int main()':
kovanice.cpp:45:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for (int i = 0; i < inp.size(); i++){
| ~~^~~~~~~~~~~~
kovanice.cpp:75:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for (int i = 0; i < edg.size(); i++){
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
592 KB |
Output is correct |
2 |
Correct |
2 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
21780 KB |
Output is correct |
2 |
Correct |
102 ms |
22652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
33 ms |
6904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
289 ms |
45292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |