#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
#define pb push_back
#define pii pair<int, int>
#define chmin(x, v) x = min(x, v)
#define chmax(x, v) x = max(x, v)
#define print(x) cout << #x << " est " << x << endl;
#define x first
#define y second
#define int long long
using namespace std;
const int N = 1e6;
int ens[N];
int getEns(int noeud){
if (ens[noeud] != noeud)
ens[noeud] = getEns(ens[noeud]);
return ens[noeud];
}
vector<int> petits[N], gros[N];
int maxChemin[N];
vector<int> ordre;
bool vu[N];
bool isWin[N];
int dfs(int noeud){
if (!vu[noeud]){
vu[noeud] = true;
for (int petit : petits[noeud])
chmax(maxChemin[noeud], dfs(petit));
maxChemin[noeud]++;
ordre.pb(noeud);
}
return maxChemin[noeud];
}
signed main(){
int nTypes, nPieces, nMesures; cin >> nTypes >> nPieces >> nMesures;
for (int i = 0; i < nPieces; ++i)
ens[i] = i;
vector<pii> aretes;
for (int i = 0; i < nMesures; ++i){
int first, second;
char op; cin >> first >> op >> second;
--first; --second;
if (op == '=')
ens[getEns(first)] = getEns(second);
else if (op == '<')
aretes.pb({second, first});
else
aretes.pb({first, second});
// cout << "salut" << endl;
}
for (pii arete : aretes){
petits[getEns(arete.first)].pb(getEns(arete.second));
gros[getEns(arete.second)].pb(getEns(arete.first));
}
for (int i = 0; i < nPieces; ++i){
if (ens[i] != i) continue;
isWin[i] = (dfs(i) == nTypes);
}
reverse(all(ordre));
for (int cur : ordre)
for (int apres : gros[cur])
isWin[cur] |= (isWin[apres] && maxChemin[apres] == maxChemin[cur] + 1);
for (int iPiece = 0; iPiece < nPieces; ++iPiece){
int nod = getEns(iPiece);
if (isWin[nod] || maxChemin[nod] > nTypes)
cout << "K" << min(nTypes, maxChemin[nod]) << endl;
else
cout << "?" << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
47316 KB |
Output is correct |
2 |
Correct |
24 ms |
47336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
353 ms |
56568 KB |
Output is correct |
2 |
Correct |
343 ms |
56748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
50748 KB |
Output is correct |
2 |
Correct |
75 ms |
51024 KB |
Output is correct |
3 |
Correct |
73 ms |
50932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
680 ms |
68836 KB |
Output is correct |
2 |
Correct |
662 ms |
72100 KB |
Output is correct |
3 |
Correct |
647 ms |
71860 KB |
Output is correct |
4 |
Correct |
702 ms |
80260 KB |
Output is correct |
5 |
Correct |
718 ms |
80856 KB |
Output is correct |