이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 150001;
vector<array<int, 2>> adj[N];
int dist[N][2][2];
int onCycle[N][2];
int p;
void dfs(int node, int type, int c)
{
//cout << "oj " << node << " " << type << " " <<dist[node][type][c] << endl;
for (int i = 0; i < adj[node].size(); i++)
{
if (adj[node].size() > 1 && i == type) continue;
if (type == 1 && i > 0) continue;
auto adjacent = adj[node][i];
//cout << "eh " << node << " " << type << " " << dist[node][type][c] << " " << adjacent[0] << endl;
int next = -1;
if (adj[adjacent[0]].size() == 1 || adj[adjacent[0]][0][1] == adjacent[1])
{
next = 0;
} else if (adj[adjacent[0]][1][1] == adjacent[1])
{
next = 1;
}
if (next != -1)
{
if (adjacent[0] == p && next == c)
{
onCycle[p][c] = dist[node][type][c] + 1;
continue;
}
dist[adjacent[0]][next][c] = dist[node][type][c] + 1;
dfs(adjacent[0], next, c);
}
}
}
void count_routes(int n, int m, int pa, int R[][2], int Q, int G[])
{
p = pa;
for (int i = 0; i < m; i++)
{
adj[R[i][0]].push_back({R[i][1], i + 1});
adj[R[i][1]].push_back({R[i][0], i + 1});
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j <= 1; j++) for (int l = 0; l <= 1; l++) dist[i][j][l] = -1;
}
dist[p][0][0] = 0;
dist[p][1][1] = 0;
dfs(p, 0, 0);
dfs(p, 1, 1);
for(int i=0; i<Q; i++)
{
int sum = 0;
int cdist = G[i];
for (int j = 0; j < n; j++)
{
//if (i == 0) cout << "a " << onCycle[p][0]<< " " << onCycle[p][1] << " " << dist[j][0][0] << " " << dist[j][0][1] << " " << j << endl;
for (int k = 0; k <= 1; k++)
{
if (dist[j][0][k] != -1)
{
if (dist[j][0][k] == cdist) sum++;
else if (onCycle[p][k] && cdist > dist[j][0][k] && dist[j][0][k] % onCycle[p][k] == cdist % onCycle[p][k])
{
sum++;
}
}
}
}
answer(sum);
}
}
/*
4 4 0
1 2
2 3
3 1
0 1
20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
*/
컴파일 시 표준 에러 (stderr) 메시지
garden.cpp: In function 'void dfs(int, int, int)':
garden.cpp:19:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for (int i = 0; i < adj[node].size(); i++)
| ~~^~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |