#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
struct Node
{
vector<Node*> n;
int dtP=INT_MAX/2;
bool dtPb;
int dtP2=INT_MAX/2;
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_MAX/2;
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)
{
if(t->n[0]==p)
{
if((t->dtP)<(INT_MAX/4)) 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/4)) 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[])
{
// 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)
{
if(i.dtP < INT_MAX/4) dtP[i.dtPb][i.dtP]++;
}
dtP[0][0]++;
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:48:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Node*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | else for(int i = 1; i < t->n.size();i++)
| ~~^~~~~~~~~~~~~
garden.cpp: In function 'void count_routes(int, int, int, int (*)[2], int, int*)':
garden.cpp:71:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Node*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(int i = 1; i < Pn.n.size(); i++)
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |