Submission #285118

#TimeUsernameProblemLanguageResultExecution timeMemory
285118PlurmBitaro’s Party (JOI18_bitaro)C++11
100 / 100
1677 ms338284 KiB
#include <bits/stdc++.h> using namespace std; const int THRESH = 400; vector<int> ord; vector<int> g[100005]; vector<int> rg[100005]; bool found[100005]; void dfs(int u){ if(found[u]) return; found[u] = true; for(int v : g[u]){ dfs(v); } ord.push_back(u); } vector<pair<int,int> > dp[100005]; // | dp[i] | <= THRESH. bool __used[100005]; vector<pair<int,int> > newVec; inline void __append(const pair<int,int>& x){ if(newVec.empty() || !__used[x.second]){ newVec.push_back(x); __used[x.second] = true; } } inline void merge(vector<pair<int,int> >& dest, vector<pair<int,int> > src){ newVec.reserve(THRESH); for(auto& v : src) v.first++; int ptr1 = 0; int ptr2 = 0; while(ptr1 < dest.size() && ptr2 < src.size() && newVec.size() < THRESH){ if(dest[ptr1] > src[ptr2]){ __append(dest[ptr1]); ptr1++; }else{ __append(src[ptr2]); ptr2++; } } while(newVec.size() < THRESH && ptr1 < dest.size()){ __append(dest[ptr1]); ptr1++; } while(newVec.size() < THRESH && ptr2 < src.size()){ __append(src[ptr2]); ptr2++; } for(auto u : newVec) __used[u.second] = false; swap(dest, newVec); newVec.clear(); } bool isBusy[100005]; int subdp[100005]; inline int recalculateAnswer(const int& t){ for(int u : ord){ subdp[u] = isBusy[u] ? -10000000 : 0; for(int v : rg[u]){ subdp[u] = max(subdp[u], subdp[v]+1); } if(u == t) return subdp[t] < 0 ? -1 : subdp[t]; } } int main(){ int n, m, q; scanf("%d%d%d",&n,&m,&q); for(int i = 0; i < m; i++){ int s,e; scanf("%d%d",&s,&e); g[s].push_back(e); rg[e].push_back(s); } for(int i = 1; i <= n; i++) dfs(i); reverse(ord.begin(), ord.end()); for(int u : ord){ for(int v : rg[u]){ merge(dp[u], dp[v]); } merge(dp[u], {make_pair(-1, u)}); } int t, y; vector<int> busy; for(int qq = 0; qq < q; qq++){ scanf("%d%d",&t,&y); busy.reserve(y); for(int i = 0; i < y; i++){ int c; scanf("%d",&c); busy.push_back(c); isBusy[c] = true; } int ans = -1; if(y < THRESH){ for(auto p : dp[t]){ if(isBusy[p.second]) continue; ans = p.first; break; } }else{ ans = recalculateAnswer(t); } for(int c : busy) isBusy[c] = false; busy.clear(); printf("%d\n",ans); } return 0; }

Compilation message (stderr)

bitaro.cpp: In function 'void merge(std::vector<std::pair<int, int> >&, std::vector<std::pair<int, int> >)':
bitaro.cpp:30:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |  while(ptr1 < dest.size() && ptr2 < src.size() && newVec.size() < THRESH){
      |        ~~~~~^~~~~~~~~~~~~
bitaro.cpp:30:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |  while(ptr1 < dest.size() && ptr2 < src.size() && newVec.size() < THRESH){
      |                              ~~~~~^~~~~~~~~~~~
bitaro.cpp:39:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |  while(newVec.size() < THRESH && ptr1 < dest.size()){
      |                                  ~~~~~^~~~~~~~~~~~~
bitaro.cpp:43:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |  while(newVec.size() < THRESH && ptr2 < src.size()){
      |                                  ~~~~~^~~~~~~~~~~~
bitaro.cpp: In function 'int main()':
bitaro.cpp:64:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   64 |  scanf("%d%d%d",&n,&m,&q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~
bitaro.cpp:67:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   67 |   scanf("%d%d",&s,&e);
      |   ~~~~~^~~~~~~~~~~~~~
bitaro.cpp:82:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   82 |   scanf("%d%d",&t,&y);
      |   ~~~~~^~~~~~~~~~~~~~
bitaro.cpp:86:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   86 |    scanf("%d",&c);
      |    ~~~~~^~~~~~~~~
bitaro.cpp: In function 'int recalculateAnswer(const int&)':
bitaro.cpp:61:1: warning: control reaches end of non-void function [-Wreturn-type]
   61 | }
      | ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...