이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct dat{
int x, f, y;
int nxt=0;
dat(){}
dat(int x, int f, int y): x(x), f(f), y(y){}
bool operator<(const dat &r)const{
return y>r.y;
}
};
int n, m, k;
vector<int> link[250002];
int cycleLen[250002];
int cycleIdx[250002];
vector<int> dist[250002];
vector<bool> visited[250002];
queue<dat> pq;
dat datList[10000000];
int datSize = 3;
void update(dat tmp){
if(tmp.x){
if(visited[tmp.x][tmp.f]) return;
visited[tmp.x][tmp.f] = 1;
dist[tmp.x][tmp.f] = tmp.y;
}
pq.push(tmp);
}
int main(){
scanf("%d %d", &n, &m);
for(int i=1; i<=m; i++){
int x, y;
scanf("%d %d", &x, &y);
link[x].push_back(y);
link[y].push_back(x);
}
scanf("%d", &k);
while(k--){
int l;
scanf("%d", &l);
for(int j=0; j<l; j++){
int tmp;
scanf("%d", &tmp);
cycleLen[tmp] = l;
cycleIdx[tmp] = j;
}
}
for(int i=1; i<=n; i++){
if(!cycleLen[i]) cycleLen[i] = 1;
dist[i] = vector<int> (cycleLen[i], 1e9);
visited[i].resize(cycleLen[i]);
}
update(dat(1, 0, 0));
int pqCnt = 0;
while(!pq.empty()){
dat tmp = pq.front(); pq.pop();
int x = tmp.x, f = tmp.f, y = tmp.y;
// printf("%d %d: %d\n", x, f, y);
// assert(f < cycleLen[x]);
if(tmp.nxt){
pq.push(datList[tmp.nxt]);
}
if(tmp.x == 0) continue;
if(x==n){
printf("%d", y);
return 0;
}
if(cycleLen[x] == 1){
for(int nxt: link[x]){
if(cycleLen[nxt] == 1) update(dat(nxt, 0, y+1));
else{
if(visited[nxt][0] || visited[nxt][1] || visited[nxt][2]) continue;
int prv = 0;
for(int j=cycleLen[nxt]-1; j>=0; j--){
int nf = (y+1+j)%cycleLen[nxt];
if(nf == cycleIdx[nxt]){
datList[datSize++] = dat(0, 0, y+j+1);
datList[datSize-1].nxt = prv;
prv = datSize-1;
}
else{
datList[datSize++] = (dat(nxt, nf, y+j+1));
datList[datSize-1].nxt = prv;
prv = datSize-1;
visited[tmp.x][tmp.f] = 1;
dist[tmp.x][tmp.f] = tmp.y;
}
}
update(datList[datSize-1]);
}
}
}
else{
for(int nxt: link[x]){
if(cycleLen[nxt] == 1) update(dat(nxt, 0, y+1));
else{
int nf = (f+1)%cycleLen[nxt];
if(nf == cycleIdx[nxt]) continue;
if(nf == cycleIdx[x] && f%cycleLen[nxt] == cycleIdx[nxt]) continue;
update(dat(nxt, nf, y+1));
}
}
}
}
printf("impossible");
}
컴파일 시 표준 에러 (stderr) 메시지
watchmen.cpp: In function 'int main()':
watchmen.cpp:66:9: warning: unused variable 'pqCnt' [-Wunused-variable]
66 | int pqCnt = 0;
| ^~~~~
watchmen.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
watchmen.cpp:41:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
watchmen.cpp:46:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | scanf("%d", &k);
| ~~~~~^~~~~~~~~~
watchmen.cpp:49:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf("%d", &l);
| ~~~~~^~~~~~~~~~
watchmen.cpp:52:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | scanf("%d", &tmp);
| ~~~~~^~~~~~~~~~~~
# | 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... |