#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
struct Node
{
vector<Node*> n;
int dtP=INT_MAX;
bool dtPb;
int dtP2=INT_MAX;
bool dtP2b;
int dtPf(Node *p)
{
if(p==n[0] && n.size()>1) return dtP2;
return dtP;
}
bool dtPfb(Node *p)
{
if(p==n[0] && n.size()>1) return dtP2b;
return dtPb;
}
};
vector<Node> nodes;
int traverse_P(Node *t,Node* p,Node* P)
{
static set<pair<Node*,bool>> seen;
bool pwasmax = (t->n[0]==p);
if(t==P) return 0;
if(seen.count({t,pwasmax})) return INT_MIN;
seen.insert({t,pwasmax});
if(pwasmax && t->n.size()>1) return traverse_P(t->n[1],t,P)+1;
else return traverse_P(t->n[0],t,P)+1;
}
void set_dtP(Node *t, Node *p,int dtP,bool dtPb)
{
int id = t-&nodes[0], pi = p-&nodes[0];
if(t->n[0]==p)
{
if(t->dtP!=INT_MAX) return;
t->dtP = dtP;
t->dtPb = dtPb;
if(t->n.size()==1)
set_dtP(t->n[0],t,dtP+1,dtPb);
else for(int i = 1; i < t->n.size();i++)
set_dtP(t->n[i],t,dtP+1,dtPb);
}
else if (t->n[1]==p)
{
if(t->dtP2!=INT_MAX) return;
t->dtP2 = dtP;
t->dtP2b = dtPb;
set_dtP(t->n[0],t,dtP+1,dtPb);
}
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
int i;
// setup graph
nodes.resize(N);
for(int i = 0; i < M; i++)
{
nodes[R[i][0]].n.push_back(&nodes[R[i][1]]);
nodes[R[i][1]].n.push_back(&nodes[R[i][0]]);
}
// traverse from P to P
Node& Pn = nodes[P];
for(int i = 1; i < Pn.n.size(); i++)
set_dtP(Pn.n[i],&Pn,1,0);
set_dtP(Pn.n[0],&Pn,1,1);
Pn.dtP = Pn.n[0]->dtPf(&Pn)+1;
Pn.dtPb = Pn.n[0]->dtPfb(&Pn);
if(Pn.n.size()>1)
{
Pn.dtP2 = Pn.n[0]->dtPf(&Pn)+1;
Pn.dtP2b = Pn.n[0]->dtPfb(&Pn);
}
array<vector<int>,2> dtP;
dtP[0].assign(N,0);
dtP[1].assign(N,0);
for(auto i : nodes)
{
dtP[i.dtPb][i.dtP]++;
}
for(int i = 0; i < Q; i++)
{
int num = G[i];
int dtPv = num;
bool dtPb = false;
int c = 0;
while(dtPv >= 0)
{
c += dtP[0][dtPv];
if(dtPb)
{
dtPv -= Pn.dtP2;
dtPb = Pn.dtP2b;
}
else
{
dtPv -= Pn.dtP;
dtPb = Pn.dtPb;
}
}
dtPv = num;
dtPb = true;
while(dtPv >= 0)
{
c += dtP[1][dtPv];
if(dtPb)
{
dtPv -= Pn.dtP2;
dtPb = Pn.dtP2b;
}
else
{
dtPv -= Pn.dtP;
dtPb = Pn.dtPb;
}
}
answer(c);
}
//for(i=0; i<N; i++) cout << i << " " << nodes[i].dtP << " " << nodes[i].dtP2 << "\n";
}
/* cases
==== work from first encounter of P
either P0 -> P0
or P0 -> P1
or neither
either P1 -> P0
or P1 -> P1
or neither
*/
Compilation message
garden.cpp: In function 'void set_dtP(Node*, Node*, int, bool)':
garden.cpp:49:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Node*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | else for(int i = 1; i < t->n.size();i++)
| ~~^~~~~~~~~~~~~
garden.cpp:41:7: warning: unused variable 'id' [-Wunused-variable]
41 | int id = t-&nodes[0], pi = p-&nodes[0];
| ^~
garden.cpp:41:25: warning: unused variable 'pi' [-Wunused-variable]
41 | int id = t-&nodes[0], pi = p-&nodes[0];
| ^~
garden.cpp: In function 'void count_routes(int, int, int, int (*)[2], int, int*)':
garden.cpp:73:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Node*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int i = 1; i < Pn.n.size(); i++)
| ~~^~~~~~~~~~~~~
garden.cpp:63:7: warning: unused variable 'i' [-Wunused-variable]
63 | int i;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |