Submission #1012264

#TimeUsernameProblemLanguageResultExecution timeMemory
1012264YassineBenYounesBitaro’s Party (JOI18_bitaro)C++17
100 / 100
603 ms110952 KiB
#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pbds tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update>
using namespace __gnu_pbds;
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (PGCD)
ll lcm(ll a , ll b) {return (a * b) / gcd(a , b);} // least common multiple (PPCM)
int dx[8] = {1, 0, 0, -1, 1, 1, -1, -1};
int dy[8] = {0, 1, -1, 0, 1, -1, -1, 1};
#define endl "\n"
#define ss second
#define ff first
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define vi vector<int>
#define vii vector<pair<int,int>>
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd  pair<double,double>
#define vdd  vector<pdd>
#define speed ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
////////////////////Only Clear Code//////////////////////////

void usaco_problem(){
    freopen("milkvisits.in", "r", stdin);
    freopen("milkvisits.out", "w", stdout);
}

void init(){
    #ifndef ONLINE_JUDGE
 
freopen("input.txt", "r", stdin);
 
freopen("output.txt", "w", stdout);
 
#endif // ONLINE_JUDGE
}
//#define int ll
const int mx = 1e5 + 5;
const int LOG = 25;
const int inf = 1e9;
const ll mod = 998244353;
const int sq = 100;

vi graph[mx];

vii far[mx];

bool taken[mx];

vii merge(vii a, vii b){
    int x = 0, y = 0;
    vii ans;
    for(pii p : a){
        taken[p.ss] = 0;
    }
    for(pii p : b){
        taken[p.ss] = 0;
    }
    for(int i = 0 ; i < b.size();i++){
        b[i].ff += 1;
    }
    while(x < a.size() && y < b.size()){
        if(a[x].ff > b[y].ff){
            ans.pb(a[x]);
            x++;
        }else{
            ans.pb(b[y]);
            y++;
        }
    }
    while(x < a.size()){
        ans.pb(a[x]);
        x++;
    }
    while(y < b.size()){
        ans.pb(b[y]);
        y++;
    }
    vii k;
    for(int i = 0;i < ans.size() && k.size() <= sq;i++){
        if(taken[ans[i].ss])continue;
        k.pb({ans[i].ff, ans[i].ss});
        taken[ans[i].ss] = 1;
    }
    return k;
}

bool no[mx];
int dp[mx];

int main(){
    speed;
    int n,m,q;cin>>n>>m>>q;
    for(int i = 0;i < m;i++){
        int a, b;cin >> a >> b;
        graph[b].pb(a);
    }
    far[1].pb({0, 1});
    for(int i = 2;i <= n;i++){
        far[i].pb({0, i});
        for(int adj : graph[i]){
            far[i] = merge(far[i], far[adj]);
        }/*cout << i << endl;
        for(pii p : far[i]){
            cout << p.ff << " " << p.ss << endl;
        }*/
    }
    while(q--){
        int st, k;cin >> st >> k;
        if(k < sq){
            vi v;
            for(int i = 0; i < k;i++){
                int a;cin >> a;v.pb(a);
                no[a] = 1;
            }
            bool ok = 0;
            for(int i = 0; i < far[st].size();i++){
                int nd = far[st][i].ss;
                if(!no[nd]){
                    ok = 1;
                    cout << far[st][i].ff << endl;
                    break;
                }
            }
            if(!ok)cout << -1 << endl;
            for(int i = 0; i < k;i++){
                no[v[i]] = 0;
            }
        }
        else{
            vi v;
            for(int i = 0; i < k;i++){
                int a;cin >> a;v.pb(a);
                no[a] = 1;
            }
            int ans = -1;
            for(int i = 1; i <= n;i++){
                dp[i] = -1;
            }
            dp[st] = 0;
            for(int i = st; i >= 1;i--){
                if(dp[i] == -1)continue;
                if(!no[i])ans = max(ans, dp[i]);
                for(int adj : graph[i]){
                    dp[adj] = max(dp[adj], dp[i] + 1);
                }
            }
            cout << ans << endl; 
            for(int x : v)no[x] = 0;
        }
    }
}

/*
    NEVER GIVE UP!
    DOING SMTHNG IS BETTER THAN DOING NTHNG!!!
    Your Guide when stuck:
    - Continue keyword only after reading the whole input
    - Don't use memset with testcases
    - Check for corner cases(n=1, n=0)
    - Check where you declare n(Be careful of declaring it globally and in main)
*/

Compilation message (stderr)

bitaro.cpp: In function 'std::vector<std::pair<int, int> > merge(std::vector<std::pair<int, int> >, std::vector<std::pair<int, int> >)':
bitaro.cpp:67:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |     for(int i = 0 ; i < b.size();i++){
      |                     ~~^~~~~~~~~~
bitaro.cpp:70: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]
   70 |     while(x < a.size() && y < b.size()){
      |           ~~^~~~~~~~~~
bitaro.cpp:70:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |     while(x < a.size() && y < b.size()){
      |                           ~~^~~~~~~~~~
bitaro.cpp:79: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]
   79 |     while(x < a.size()){
      |           ~~^~~~~~~~~~
bitaro.cpp:83: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]
   83 |     while(y < b.size()){
      |           ~~^~~~~~~~~~
bitaro.cpp:88:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |     for(int i = 0;i < ans.size() && k.size() <= sq;i++){
      |                   ~~^~~~~~~~~~~~
bitaro.cpp: In function 'int main()':
bitaro.cpp:125:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  125 |             for(int i = 0; i < far[st].size();i++){
      |                            ~~^~~~~~~~~~~~~~~~
bitaro.cpp: In function 'void usaco_problem()':
bitaro.cpp:32:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |     freopen("milkvisits.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bitaro.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |     freopen("milkvisits.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bitaro.cpp: In function 'void init()':
bitaro.cpp:39:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 | freopen("input.txt", "r", stdin);
      | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
bitaro.cpp:41:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 | freopen("output.txt", "w", stdout);
      | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...