이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
#define int long long
const int maxn = 5005;
const int SHORT_MAX = 32767;
int n, m, k;
short vis[maxn];
vector<int>adj[maxn];
short dist[maxn][maxn][2];
signed main()
{
cin.tie(0) -> sync_with_stdio(0);
cin >> n >> m >> k;
for(int i = 0; i < m; i++){
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
dist[i][j][0] = SHORT_MAX;
dist[j][i][0] = SHORT_MAX;
dist[i][j][1] = SHORT_MAX;
dist[j][i][1] = SHORT_MAX;
}
}
for(int s = 1; s <= n; s++){
dist[s][s][0] = 0;
queue<int>q;
q.push(s);
while(q.size()){
int v = q.front();
q.pop();
for(int i : adj[v]){
if(dist[s][v][0] + 1 < dist[s][i][1]){
dist[s][i][1] = dist[s][v][0] + 1;
q.push(i);
}
if(dist[s][v][1] + 1 < dist[s][i][0]){
dist[s][i][0] = dist[s][v][1] + 1;
q.push(i);
}
}
}
}
while(k--){
bool valid = 0;
int s, t, d;
cin >> s >> t >> d;
if(s == t){
for(int i : adj[s]){
if(dist[s][i][1] != SHORT_MAX) valid = true;
}
if(valid) cout << "TAK\n";
else cout << "NIE\n";
continue;
}
else if(d % 2 == 0){
if(dist[s][t][0] != SHORT_MAX && dist[s][t][0] <= d){
cout << "TAK\n";
valid = 1;
}
else if(dist[t][s][0] != SHORT_MAX && dist[t][s][0] <= d){
cout << "TAK\n";
valid = 1;
}
}
else if(d % 2 == 1){
if(dist[s][t][1] != SHORT_MAX && dist[s][t][1] <= d){
cout << "TAK\n";
valid = 1;
}
else if(dist[t][s][1] != SHORT_MAX && dist[t][s][1] <= d){
cout << "TAK\n";
valid = 1;
}
}
if(!valid) cout << "NIE\n";
}
}
# | 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... |
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |