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<bits/stdc++.h>
#define maxn 100010
#define f first
#define s second
#define ll long long
using namespace std;
ll n,m,q;
vector<ll> edges[maxn];
bool blocked[maxn];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>q;
for(ll i=0;i<m;i++)
{
ll a,b;
cin>>a>>b;
if(a<b) swap(a,b);
edges[a].push_back(b);
}
ll B=100;
vector<pair<ll,ll>> longest[n+1];
vector<ll> dist(n+1,-1);
for(ll i=1;i<=n;i++)
{
// longest[i].push_back({0,i});
vector<ll> from;
vector<pair<ll,ll>> curr;
curr.push_back({0,i});
for(auto e:edges[i])
{
for(auto fff:longest[e])
{
ll way=fff.f;
ll ver=fff.s;
if(dist[ver]==-1)
{
from.push_back(ver);
dist[ver]=way+1;
}
else dist[ver]=max(dist[ver],way+1);
}
}
for(auto e:from) curr.push_back({dist[e],e});
sort(curr.begin(),curr.end());
reverse(curr.begin(),curr.end());
ll idx=0;
while(idx<B && idx<curr.size())
{
longest[i].push_back({curr[idx].f,curr[idx].s});
idx++;
}
for(auto e:from) dist[e]=-1;
from.clear();
curr.clear();
}
for(ll i=0;i<q;i++)
{
ll start,y;
cin>>start>>y;
vector<ll> c(y);
for(ll j=0;j<y;j++)
{
cin>>c[j];
blocked[c[j]]=1;
}
ll ans=-1;
if(y>=B-1)
{
vector<ll> dp(start+1,-INT_MAX);
dp[start]=0;
for(ll j=start;j>0;j--)
{
if(dp[j]==-INT_MAX) continue;
if(!blocked[j]) ans=max(ans,dp[j]);
for(auto e:edges[j]) dp[e]=max(dp[e],dp[j]+1);
}
}
if(y<B-1)
{
for(ll j=0;j<longest[start].size();j++)
{
if(!blocked[longest[start][j].s]) {ans=longest[start][j].f;break;}
}
}
cout<<ans<<"\n";
for(auto e:c) blocked[e]=0;
}
}
Compilation message (stderr)
bitaro.cpp: In function 'int main()':
bitaro.cpp:55:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | while(idx<B && idx<curr.size())
| ~~~^~~~~~~~~~~~
bitaro.cpp:90:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | for(ll j=0;j<longest[start].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... |