답안 #1084910

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1084910 2024-09-07T08:12:59 Z DucNguyen2007 Bitaro’s Party (JOI18_bitaro) C++14
100 / 100
1175 ms 420812 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())
      |            ~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5464 KB Output is correct
2 Correct 2 ms 5464 KB Output is correct
3 Correct 3 ms 5468 KB Output is correct
4 Correct 2 ms 5468 KB Output is correct
5 Correct 4 ms 5692 KB Output is correct
6 Correct 3 ms 5788 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 5 ms 5720 KB Output is correct
11 Correct 3 ms 5724 KB Output is correct
12 Correct 4 ms 5724 KB Output is correct
13 Correct 3 ms 5724 KB Output is correct
14 Correct 4 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 4 ms 5724 KB Output is correct
19 Correct 4 ms 5680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5464 KB Output is correct
2 Correct 2 ms 5464 KB Output is correct
3 Correct 3 ms 5468 KB Output is correct
4 Correct 2 ms 5468 KB Output is correct
5 Correct 4 ms 5692 KB Output is correct
6 Correct 3 ms 5788 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 5 ms 5720 KB Output is correct
11 Correct 3 ms 5724 KB Output is correct
12 Correct 4 ms 5724 KB Output is correct
13 Correct 3 ms 5724 KB Output is correct
14 Correct 4 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 4 ms 5724 KB Output is correct
19 Correct 4 ms 5680 KB Output is correct
20 Correct 92 ms 8272 KB Output is correct
21 Correct 91 ms 8548 KB Output is correct
22 Correct 96 ms 8272 KB Output is correct
23 Correct 93 ms 8280 KB Output is correct
24 Correct 605 ms 268776 KB Output is correct
25 Correct 621 ms 279340 KB Output is correct
26 Correct 629 ms 278948 KB Output is correct
27 Correct 883 ms 419268 KB Output is correct
28 Correct 883 ms 419264 KB Output is correct
29 Correct 941 ms 419024 KB Output is correct
30 Correct 892 ms 413600 KB Output is correct
31 Correct 870 ms 402716 KB Output is correct
32 Correct 922 ms 413396 KB Output is correct
33 Correct 595 ms 260680 KB Output is correct
34 Correct 535 ms 216900 KB Output is correct
35 Correct 606 ms 258764 KB Output is correct
36 Correct 760 ms 338384 KB Output is correct
37 Correct 727 ms 311036 KB Output is correct
38 Correct 751 ms 339192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5464 KB Output is correct
2 Correct 2 ms 5464 KB Output is correct
3 Correct 3 ms 5468 KB Output is correct
4 Correct 2 ms 5468 KB Output is correct
5 Correct 4 ms 5692 KB Output is correct
6 Correct 3 ms 5788 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 5 ms 5720 KB Output is correct
11 Correct 3 ms 5724 KB Output is correct
12 Correct 4 ms 5724 KB Output is correct
13 Correct 3 ms 5724 KB Output is correct
14 Correct 4 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 4 ms 5724 KB Output is correct
19 Correct 4 ms 5680 KB Output is correct
20 Correct 92 ms 8272 KB Output is correct
21 Correct 91 ms 8548 KB Output is correct
22 Correct 96 ms 8272 KB Output is correct
23 Correct 93 ms 8280 KB Output is correct
24 Correct 605 ms 268776 KB Output is correct
25 Correct 621 ms 279340 KB Output is correct
26 Correct 629 ms 278948 KB Output is correct
27 Correct 883 ms 419268 KB Output is correct
28 Correct 883 ms 419264 KB Output is correct
29 Correct 941 ms 419024 KB Output is correct
30 Correct 892 ms 413600 KB Output is correct
31 Correct 870 ms 402716 KB Output is correct
32 Correct 922 ms 413396 KB Output is correct
33 Correct 595 ms 260680 KB Output is correct
34 Correct 535 ms 216900 KB Output is correct
35 Correct 606 ms 258764 KB Output is correct
36 Correct 760 ms 338384 KB Output is correct
37 Correct 727 ms 311036 KB Output is correct
38 Correct 751 ms 339192 KB Output is correct
39 Correct 628 ms 272244 KB Output is correct
40 Correct 600 ms 274588 KB Output is correct
41 Correct 605 ms 276248 KB Output is correct
42 Correct 804 ms 274388 KB Output is correct
43 Correct 624 ms 273868 KB Output is correct
44 Correct 108 ms 9808 KB Output is correct
45 Correct 96 ms 8784 KB Output is correct
46 Correct 327 ms 8724 KB Output is correct
47 Correct 116 ms 8612 KB Output is correct
48 Correct 105 ms 8500 KB Output is correct
49 Correct 932 ms 419976 KB Output is correct
50 Correct 920 ms 418768 KB Output is correct
51 Correct 106 ms 9652 KB Output is correct
52 Correct 327 ms 8796 KB Output is correct
53 Correct 795 ms 337868 KB Output is correct
54 Correct 784 ms 311756 KB Output is correct
55 Correct 758 ms 337100 KB Output is correct
56 Correct 746 ms 311832 KB Output is correct
57 Correct 107 ms 9476 KB Output is correct
58 Correct 114 ms 9640 KB Output is correct
59 Correct 330 ms 8784 KB Output is correct
60 Correct 292 ms 8788 KB Output is correct
61 Correct 1000 ms 418760 KB Output is correct
62 Correct 904 ms 338388 KB Output is correct
63 Correct 890 ms 309964 KB Output is correct
64 Correct 1175 ms 418768 KB Output is correct
65 Correct 1164 ms 337100 KB Output is correct
66 Correct 1040 ms 312012 KB Output is correct
67 Correct 886 ms 418616 KB Output is correct
68 Correct 762 ms 338676 KB Output is correct
69 Correct 716 ms 308116 KB Output is correct
70 Correct 976 ms 419108 KB Output is correct
71 Correct 781 ms 338560 KB Output is correct
72 Correct 722 ms 311508 KB Output is correct
73 Correct 929 ms 418928 KB Output is correct
74 Correct 814 ms 338444 KB Output is correct
75 Correct 734 ms 311108 KB Output is correct
76 Correct 946 ms 420812 KB Output is correct
77 Correct 927 ms 419280 KB Output is correct
78 Correct 904 ms 419532 KB Output is correct
79 Correct 109 ms 9688 KB Output is correct
80 Correct 283 ms 8760 KB Output is correct
81 Correct 95 ms 8532 KB Output is correct
82 Correct 936 ms 415064 KB Output is correct
83 Correct 913 ms 404164 KB Output is correct
84 Correct 890 ms 413636 KB Output is correct
85 Correct 882 ms 402636 KB Output is correct
86 Correct 979 ms 413908 KB Output is correct
87 Correct 897 ms 403308 KB Output is correct
88 Correct 108 ms 9808 KB Output is correct
89 Correct 108 ms 9648 KB Output is correct
90 Correct 300 ms 8792 KB Output is correct
91 Correct 291 ms 8816 KB Output is correct
92 Correct 95 ms 8280 KB Output is correct
93 Correct 97 ms 8528 KB Output is correct
94 Correct 639 ms 261892 KB Output is correct
95 Correct 548 ms 217472 KB Output is correct
96 Correct 626 ms 259428 KB Output is correct
97 Correct 532 ms 220296 KB Output is correct
98 Correct 643 ms 260552 KB Output is correct
99 Correct 572 ms 219340 KB Output is correct
100 Correct 110 ms 9808 KB Output is correct
101 Correct 108 ms 9556 KB Output is correct
102 Correct 319 ms 8884 KB Output is correct
103 Correct 294 ms 8768 KB Output is correct
104 Correct 107 ms 8276 KB Output is correct
105 Correct 97 ms 8272 KB Output is correct
106 Correct 789 ms 338972 KB Output is correct
107 Correct 718 ms 312784 KB Output is correct
108 Correct 770 ms 339692 KB Output is correct
109 Correct 699 ms 311028 KB Output is correct
110 Correct 773 ms 339948 KB Output is correct
111 Correct 706 ms 312016 KB Output is correct
112 Correct 110 ms 9776 KB Output is correct
113 Correct 111 ms 9812 KB Output is correct
114 Correct 294 ms 8872 KB Output is correct
115 Correct 289 ms 8784 KB Output is correct
116 Correct 94 ms 8508 KB Output is correct
117 Correct 103 ms 8364 KB Output is correct
118 Correct 937 ms 418764 KB Output is correct
119 Correct 756 ms 338904 KB Output is correct
120 Correct 692 ms 310476 KB Output is correct
121 Correct 901 ms 418664 KB Output is correct
122 Correct 768 ms 337504 KB Output is correct
123 Correct 725 ms 310464 KB Output is correct
124 Correct 939 ms 418808 KB Output is correct
125 Correct 803 ms 339284 KB Output is correct
126 Correct 771 ms 309684 KB Output is correct