Submission #1084912

# Submission time Handle Problem Language Result Execution time Memory
1084912 2024-09-07T08:14:50 Z vjudge1 Bitaro’s Party (JOI18_bitaro) C++17
100 / 100
1174 ms 419024 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
const int maxN=1e5+5;
const ll inf=2e18;
int n,m,q,dp[maxN+1],block,a[maxN+1],cnt[maxN+1];
vector<int> adj[maxN+1],topo;
vector<pii> vec[maxN+1];
bool vis[maxN+1];
void dfs(int u)
{
    vis[u]=true;
    for(auto v:adj[u])
    {
        if(!vis[v])
        {
            dfs(v);
        }
    }
    topo.push_back(u);
}
void meg(int u,int v)
{
    vector<pii> vt;
    int i=0,j=0;
    for(auto i:vec[u])
    {
        vis[i.fi]=false;
    }
    for(auto i:vec[v])
    {
        vis[i.fi]=false;
    }
    while(i<vec[u].size()&&j<vec[v].size()&&vt.size()<block)
    {
        while(i<vec[u].size()&&vis[vec[u][i].fi])
        {
            i++;
        }
        while(j<vec[v].size()&&vis[vec[v][j].fi])
        {
            j++;
        }
        if(i>=vec[u].size()||j>=vec[v].size())
        {
            break;
        }
        int p1=vec[u][i].se,p2=vec[v][j].se;
        if(p1+1>p2)
        {
            vt.push_back({vec[u][i].fi,p1+1});
            vis[vec[u][i].fi]=true;
            i++;
        }
        else
        {
            vt.push_back({vec[v][j].fi,p2});
            vis[vec[v][j].fi]=true;
            j++;
        }
    }
    while(i<vec[u].size()&&vt.size()<block)
    {
        while(i<vec[u].size()&&vis[vec[u][i].fi])
        {
            i++;
        }
        if(i>=vec[u].size())
        {
            break;
        }
        vt.push_back({vec[u][i].fi,vec[u][i].se+1});
        i++;
    }
    while(j<vec[v].size()&&vt.size()<block)
    {
        while(j<vec[v].size()&&vis[vec[v][j].fi])
        {
            j++;
        }
        if(j>=vec[v].size())
        {
            break;
        }
        vt.push_back({vec[v][j].fi,vec[v][j].se});
        j++;
    }
    swap(vec[v],vt);
}
void make()
{
    for(auto u:topo)
    {
        vec[u].push_back({u,0});
        for(auto v:adj[u])
        {
            //cout<<u<<" "<<v<<'\n';
            meg(u,v);
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m>>q;
    for(int i=1;i<=m;i++)
    {
        int u,v;
        cin>>u>>v;
        adj[u].push_back(v);
    }
    block=sqrt(n);
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
        {
            dfs(i);
        }
    }
    memset(dp,0,sizeof(dp));
    reverse(topo.begin(),topo.end());
    make();
    while(q--)
    {
        int t,y;
        cin>>t>>y;
        if(y>=block)
        {
            for(int i=1;i<=n;i++)
            {
                dp[i]=0;
            }
        }
        for(int i=1;i<=y;i++)
        {
            int u;
            cin>>u;
            a[i]=u;
            dp[u]=INT_MIN;
            if(y<block)
            {
                cnt[u]++;
            }
        }
        if(y>=block)
        {
            for(auto u:topo)
            {
                for(auto v:adj[u])
                {
                    dp[v]=max(dp[v],dp[u]+1);
                }
            }
            if(dp[t]<0)
            {
                cout<<-1;
            }
            else cout<<dp[t];
        }
        else
        {
            int ans=-1;
            for(auto u:vec[t])
            {
                if(!cnt[u.fi])
                {
                    ans=u.se;
                    break;
                }
            }
            cout<<ans;
        }
        cout<<'\n';
        for(int i=1;i<=y;i++)
        {
            cnt[a[i]]=0;
        }
    }
}

Compilation message

bitaro.cpp: In function 'void meg(int, int)':
bitaro.cpp:38:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |     while(i<vec[u].size()&&j<vec[v].size()&&vt.size()<block)
      |           ~^~~~~~~~~~~~~~
bitaro.cpp:38: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]
   38 |     while(i<vec[u].size()&&j<vec[v].size()&&vt.size()<block)
      |                            ~^~~~~~~~~~~~~~
bitaro.cpp:38:54: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   38 |     while(i<vec[u].size()&&j<vec[v].size()&&vt.size()<block)
      |                                             ~~~~~~~~~^~~~~~
bitaro.cpp:40:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |         while(i<vec[u].size()&&vis[vec[u][i].fi])
      |               ~^~~~~~~~~~~~~~
bitaro.cpp:44:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |         while(j<vec[v].size()&&vis[vec[v][j].fi])
      |               ~^~~~~~~~~~~~~~
bitaro.cpp:48: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]
   48 |         if(i>=vec[u].size()||j>=vec[v].size())
      |            ~^~~~~~~~~~~~~~~
bitaro.cpp:48:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |         if(i>=vec[u].size()||j>=vec[v].size())
      |                              ~^~~~~~~~~~~~~~~
bitaro.cpp:66:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |     while(i<vec[u].size()&&vt.size()<block)
      |           ~^~~~~~~~~~~~~~
bitaro.cpp:66:37: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   66 |     while(i<vec[u].size()&&vt.size()<block)
      |                            ~~~~~~~~~^~~~~~
bitaro.cpp:68:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |         while(i<vec[u].size()&&vis[vec[u][i].fi])
      |               ~^~~~~~~~~~~~~~
bitaro.cpp:72: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]
   72 |         if(i>=vec[u].size())
      |            ~^~~~~~~~~~~~~~~
bitaro.cpp:79:12: 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(j<vec[v].size()&&vt.size()<block)
      |           ~^~~~~~~~~~~~~~
bitaro.cpp:79:37: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   79 |     while(j<vec[v].size()&&vt.size()<block)
      |                            ~~~~~~~~~^~~~~~
bitaro.cpp:81:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |         while(j<vec[v].size()&&vis[vec[v][j].fi])
      |               ~^~~~~~~~~~~~~~
bitaro.cpp:85: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]
   85 |         if(j>=vec[v].size())
      |            ~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5464 KB Output is correct
2 Correct 3 ms 5464 KB Output is correct
3 Correct 2 ms 5464 KB Output is correct
4 Correct 2 ms 5468 KB Output is correct
5 Correct 4 ms 5724 KB Output is correct
6 Correct 3 ms 5720 KB Output is correct
7 Correct 3 ms 5724 KB Output is correct
8 Correct 3 ms 5720 KB Output is correct
9 Correct 4 ms 5724 KB Output is correct
10 Correct 3 ms 5720 KB Output is correct
11 Correct 4 ms 5812 KB Output is correct
12 Correct 4 ms 5720 KB Output is correct
13 Correct 4 ms 5724 KB Output is correct
14 Correct 3 ms 5724 KB Output is correct
15 Correct 3 ms 5724 KB Output is correct
16 Correct 3 ms 5724 KB Output is correct
17 Correct 3 ms 5724 KB Output is correct
18 Correct 3 ms 5724 KB Output is correct
19 Correct 4 ms 5724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5464 KB Output is correct
2 Correct 3 ms 5464 KB Output is correct
3 Correct 2 ms 5464 KB Output is correct
4 Correct 2 ms 5468 KB Output is correct
5 Correct 4 ms 5724 KB Output is correct
6 Correct 3 ms 5720 KB Output is correct
7 Correct 3 ms 5724 KB Output is correct
8 Correct 3 ms 5720 KB Output is correct
9 Correct 4 ms 5724 KB Output is correct
10 Correct 3 ms 5720 KB Output is correct
11 Correct 4 ms 5812 KB Output is correct
12 Correct 4 ms 5720 KB Output is correct
13 Correct 4 ms 5724 KB Output is correct
14 Correct 3 ms 5724 KB Output is correct
15 Correct 3 ms 5724 KB Output is correct
16 Correct 3 ms 5724 KB Output is correct
17 Correct 3 ms 5724 KB Output is correct
18 Correct 3 ms 5724 KB Output is correct
19 Correct 4 ms 5724 KB Output is correct
20 Correct 91 ms 7764 KB Output is correct
21 Correct 100 ms 8060 KB Output is correct
22 Correct 92 ms 7988 KB Output is correct
23 Correct 95 ms 7772 KB Output is correct
24 Correct 580 ms 267732 KB Output is correct
25 Correct 633 ms 278344 KB Output is correct
26 Correct 612 ms 277964 KB Output is correct
27 Correct 912 ms 418256 KB Output is correct
28 Correct 897 ms 418512 KB Output is correct
29 Correct 903 ms 418172 KB Output is correct
30 Correct 940 ms 412620 KB Output is correct
31 Correct 900 ms 401748 KB Output is correct
32 Correct 1050 ms 412388 KB Output is correct
33 Correct 642 ms 259520 KB Output is correct
34 Correct 580 ms 215848 KB Output is correct
35 Correct 674 ms 257684 KB Output is correct
36 Correct 788 ms 337468 KB Output is correct
37 Correct 734 ms 310172 KB Output is correct
38 Correct 805 ms 338380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5464 KB Output is correct
2 Correct 3 ms 5464 KB Output is correct
3 Correct 2 ms 5464 KB Output is correct
4 Correct 2 ms 5468 KB Output is correct
5 Correct 4 ms 5724 KB Output is correct
6 Correct 3 ms 5720 KB Output is correct
7 Correct 3 ms 5724 KB Output is correct
8 Correct 3 ms 5720 KB Output is correct
9 Correct 4 ms 5724 KB Output is correct
10 Correct 3 ms 5720 KB Output is correct
11 Correct 4 ms 5812 KB Output is correct
12 Correct 4 ms 5720 KB Output is correct
13 Correct 4 ms 5724 KB Output is correct
14 Correct 3 ms 5724 KB Output is correct
15 Correct 3 ms 5724 KB Output is correct
16 Correct 3 ms 5724 KB Output is correct
17 Correct 3 ms 5724 KB Output is correct
18 Correct 3 ms 5724 KB Output is correct
19 Correct 4 ms 5724 KB Output is correct
20 Correct 91 ms 7764 KB Output is correct
21 Correct 100 ms 8060 KB Output is correct
22 Correct 92 ms 7988 KB Output is correct
23 Correct 95 ms 7772 KB Output is correct
24 Correct 580 ms 267732 KB Output is correct
25 Correct 633 ms 278344 KB Output is correct
26 Correct 612 ms 277964 KB Output is correct
27 Correct 912 ms 418256 KB Output is correct
28 Correct 897 ms 418512 KB Output is correct
29 Correct 903 ms 418172 KB Output is correct
30 Correct 940 ms 412620 KB Output is correct
31 Correct 900 ms 401748 KB Output is correct
32 Correct 1050 ms 412388 KB Output is correct
33 Correct 642 ms 259520 KB Output is correct
34 Correct 580 ms 215848 KB Output is correct
35 Correct 674 ms 257684 KB Output is correct
36 Correct 788 ms 337468 KB Output is correct
37 Correct 734 ms 310172 KB Output is correct
38 Correct 805 ms 338380 KB Output is correct
39 Correct 653 ms 270660 KB Output is correct
40 Correct 620 ms 273380 KB Output is correct
41 Correct 668 ms 275192 KB Output is correct
42 Correct 837 ms 273412 KB Output is correct
43 Correct 651 ms 272784 KB Output is correct
44 Correct 134 ms 8808 KB Output is correct
45 Correct 98 ms 8272 KB Output is correct
46 Correct 377 ms 8092 KB Output is correct
47 Correct 118 ms 7764 KB Output is correct
48 Correct 96 ms 7760 KB Output is correct
49 Correct 956 ms 418420 KB Output is correct
50 Correct 948 ms 417612 KB Output is correct
51 Correct 127 ms 8528 KB Output is correct
52 Correct 360 ms 8276 KB Output is correct
53 Correct 831 ms 336552 KB Output is correct
54 Correct 790 ms 310320 KB Output is correct
55 Correct 808 ms 336088 KB Output is correct
56 Correct 750 ms 310860 KB Output is correct
57 Correct 123 ms 8596 KB Output is correct
58 Correct 117 ms 8444 KB Output is correct
59 Correct 342 ms 8276 KB Output is correct
60 Correct 371 ms 8132 KB Output is correct
61 Correct 1010 ms 417736 KB Output is correct
62 Correct 912 ms 337356 KB Output is correct
63 Correct 839 ms 308804 KB Output is correct
64 Correct 1174 ms 417768 KB Output is correct
65 Correct 1144 ms 336076 KB Output is correct
66 Correct 1056 ms 310852 KB Output is correct
67 Correct 892 ms 417632 KB Output is correct
68 Correct 755 ms 337412 KB Output is correct
69 Correct 700 ms 306900 KB Output is correct
70 Correct 912 ms 417900 KB Output is correct
71 Correct 769 ms 337240 KB Output is correct
72 Correct 744 ms 310352 KB Output is correct
73 Correct 958 ms 417528 KB Output is correct
74 Correct 801 ms 337200 KB Output is correct
75 Correct 734 ms 309900 KB Output is correct
76 Correct 922 ms 419024 KB Output is correct
77 Correct 914 ms 418256 KB Output is correct
78 Correct 900 ms 418252 KB Output is correct
79 Correct 108 ms 8808 KB Output is correct
80 Correct 296 ms 8316 KB Output is correct
81 Correct 92 ms 8036 KB Output is correct
82 Correct 980 ms 413384 KB Output is correct
83 Correct 886 ms 402600 KB Output is correct
84 Correct 909 ms 412612 KB Output is correct
85 Correct 873 ms 401544 KB Output is correct
86 Correct 937 ms 412620 KB Output is correct
87 Correct 890 ms 402332 KB Output is correct
88 Correct 110 ms 8784 KB Output is correct
89 Correct 124 ms 8732 KB Output is correct
90 Correct 295 ms 8020 KB Output is correct
91 Correct 298 ms 8272 KB Output is correct
92 Correct 93 ms 8028 KB Output is correct
93 Correct 94 ms 8024 KB Output is correct
94 Correct 628 ms 260364 KB Output is correct
95 Correct 539 ms 215812 KB Output is correct
96 Correct 672 ms 258760 KB Output is correct
97 Correct 525 ms 219084 KB Output is correct
98 Correct 675 ms 259528 KB Output is correct
99 Correct 551 ms 219080 KB Output is correct
100 Correct 110 ms 9556 KB Output is correct
101 Correct 113 ms 9556 KB Output is correct
102 Correct 278 ms 8540 KB Output is correct
103 Correct 298 ms 8744 KB Output is correct
104 Correct 95 ms 8284 KB Output is correct
105 Correct 95 ms 8504 KB Output is correct
106 Correct 799 ms 338380 KB Output is correct
107 Correct 752 ms 312272 KB Output is correct
108 Correct 863 ms 339668 KB Output is correct
109 Correct 717 ms 310644 KB Output is correct
110 Correct 838 ms 339664 KB Output is correct
111 Correct 735 ms 311720 KB Output is correct
112 Correct 112 ms 9556 KB Output is correct
113 Correct 110 ms 9556 KB Output is correct
114 Correct 309 ms 8784 KB Output is correct
115 Correct 289 ms 8528 KB Output is correct
116 Correct 95 ms 8276 KB Output is correct
117 Correct 101 ms 8492 KB Output is correct
118 Correct 914 ms 418368 KB Output is correct
119 Correct 760 ms 338624 KB Output is correct
120 Correct 718 ms 310224 KB Output is correct
121 Correct 926 ms 418512 KB Output is correct
122 Correct 758 ms 337288 KB Output is correct
123 Correct 706 ms 310192 KB Output is correct
124 Correct 901 ms 418564 KB Output is correct
125 Correct 864 ms 339032 KB Output is correct
126 Correct 714 ms 309344 KB Output is correct