#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
using namespace std;
const int mxn = 1.5e5 + 1000;
const int MXN = 3e5 + 100000;
typedef pair<int, int> pii;
#define ff first
#define ss second
#define MP make_pair
vector<int> adj[mxn]; // OG graph
vector<int> rev[MXN]; // buba graph
int dis[MXN], nxt[MXN];
bool vis[MXN];
bool fake(int cur, int prv) {
if(adj[cur].size() == 1) return 0;
return prv == adj[cur][0];
}
void build_buba(int cur, int prv) {
int bu = cur + mxn * fake(cur, prv);
// cout << prv << " -> " << cur << " (" << fake(cur, prv) << ")\n";
if(vis[bu]) return;
vis[bu] = 1;
int sus = 0;
if(adj[cur].size() == 1) sus = adj[cur][0];
else if(prv == adj[cur][0]) sus = adj[cur][1];
else sus = adj[cur][0];
int bv = sus + mxn * fake(sus, cur);
nxt[bu] = bv;
rev[bv].push_back(bu);
build_buba(sus, cur);
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
P++;
for(int i = 0; i < M; i++) {
int u = R[i][0] + 1;
int v = R[i][1] + 1;
adj[u].push_back(v);
adj[v].push_back(u);
}
// for(int i = 1; i <= N; i++) {
// cout << i << ": ";
// for(int j : adj[i])
// cout << j << ' ';
// cout << endl;
// }
for(int i = 1; i <= N; i++)
build_buba(i, 0);
memset(vis, 0, sizeof vis);
vector<int> path, cyc;
int node = 1;
while(!vis[node]) {
vis[node] = 1;
path.push_back(node);
node = nxt[node];
}
// cout << "path: ";
// for(int u : path)
// cout << u << ' ';
// cout << endl;
// cout << "node = " << node << endl;
while(path.back() != node) {
int u = path.back();
cyc.push_back(u);
path.pop_back();
}
cyc.push_back(node);
// cout << "cyc: ";
// for(int u : cyc)
// cout << u << ' ';
// cout << endl << "size = " << cyc.size() << endl;
priority_queue<pii> pq;
pq.push(MP(0, P));
memset(dis, -1, sizeof dis);
while(pq.size()) {
int d = -pq.top().ff;
int u = pq.top().ss;
pq.pop();
if(dis[u] > -1) continue;
dis[u] = d;
for(int v : rev[u])
if(!dis[v] && v != P)
pq.push(MP(-d-1, v));
}
for(int i = 0; i < Q; i++) {
int D = G[i], ans = 0;
for(int u = 1; u <= N; u++) {
if(dis[u] == -1) continue;
if(dis[u] > D) continue;
int extra = dis[u] - D;
if(extra % cyc.size() == 0)
ans++;
}
answer(ans);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
15196 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
15196 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
15196 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |