This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
int dist[300005], dist2[300005], ind[150005], ind2[150005], mn[150005], mn2[150005], ans[2005];
vector<int> vec[300005], tor[300005], tor2[300005];
vector<pair<int, int> > adj[150005];
queue<int> q;
void count_routes(int n, int m, int p, int r[][2], int k, int g[])
{
for (int i=0; i<m; i++)
{
adj[r[i][0]].push_back({r[i][1], i});
adj[r[i][1]].push_back({r[i][0], i});
}
for (int i=0; i<n; i++)
{
ind[i]=adj[i][0].first;
mn[i]=adj[i][0].second;
if (adj[i].size()==1)
{
ind2[i]=adj[i][0].first;
mn2[i]=adj[i][0].second;
}
else
{
mn2[i]=1e9;
for (int j=1; j<adj[i].size(); j++)
{
// cout << "i j " << i << ' ' << j << ' ' << adj[i][j].first << ' ' << adj[i][j].second << '\n';
if (mn[i]>adj[i][j].second)
{
ind2[i]=ind[i];
mn2[i]=mn[i];
ind[i]=adj[i][j].first;
mn[i]=adj[i][j].second;
}
else if (mn2[i]>adj[i][j].second)
{
ind2[i]=adj[i][j].first;
mn2[i]=adj[i][j].second;
}
}
// cout << "ind " << i << ": " << ind[i] << ' ' << ind2[i] << '\n';
}
}
for (int i=0; i<n; i++)
{
if (ind[ind[i]]==i && ind2[ind[i]]!=i)
vec[ind[i]+n].push_back(i);
else
vec[ind[i]].push_back(i);
if (ind[ind2[i]]==i && ind2[ind2[i]]!=i)
vec[ind2[i]+n].push_back(i+n);
else
vec[ind2[i]].push_back(i+n);
}
// for (int i=0; i<n*2; i++)
// for (int v:vec[i])
// cout << v << ' ' << i << '\n';
q.push(p);
while (!q.empty())
{
int u=q.front();
q.pop();
// cout << "u=" << u << '\n';
for (int v:vec[u])
{
if (!dist[v])
{
dist[v]=dist[u]+1;
q.push(v);
}
}
}
q.push(p+n);
while (!q.empty())
{
int u=q.front();
q.pop();
// cout << "u=" << u << '\n';
for (int v:vec[u])
{
if (!dist2[v])
{
dist2[v]=dist2[u]+1;
q.push(v);
}
}
}
if (dist[p])
{
for (int i=0; i<n; i++)
if (dist[i])
tor[dist[i]%dist[p]].push_back(dist[i]);
for (int i=0; i<dist[p]; i++)
sort(tor[i].begin(), tor[i].end());
for (int i=0; i<k; i++)
{
int x=g[i]%dist[p];
ans[i]+=upper_bound(tor[x].begin(), tor[x].end(), g[i])-tor[x].begin();
}
}
else
{
for (int i=0; i<n; i++)
if (dist[i])
tor[dist[i]].push_back(dist[i]);
for (int i=0; i<k; i++)
if (g[i]<=n*2)
ans[i]+=tor[g[i]].size();
}
if (dist2[p+n])
{
for (int i=0; i<n; i++)
if (dist2[i])
tor2[dist2[i]%dist2[p+n]].push_back(dist2[i]);
for (int i=0; i<dist2[p+n]; i++)
sort(tor2[i].begin(), tor2[i].end());
for (int i=0; i<k; i++)
{
int x=g[i]%dist2[p+n];
ans[i]+=upper_bound(tor2[x].begin(), tor2[x].end(), g[i])-tor2[x].begin();
}
}
else
{
for (int i=0; i<n; i++)
if (dist2[i])
tor2[dist2[i]].push_back(dist2[i]);
for (int i=0; i<k; i++)
if (g[i]<=n*2)
ans[i]+=tor2[g[i]].size();
}
for (int i=0; i<k; i++)
answer(ans[i]);
}
Compilation message (stderr)
garden.cpp: In function 'void count_routes(int, int, int, int (*)[2], int, int*)':
garden.cpp:28:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for (int j=1; j<adj[i].size(); j++)
| ~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |