Submission #1099202

# Submission time Handle Problem Language Result Execution time Memory
1099202 2024-10-10T17:57:29 Z MMihalev Highway Tolls (IOI18_highway) C++14
100 / 100
180 ms 20104 KB
#include "highway.h"
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int MAX_N=90005;
int q;
int remedges;
int n,m;
vector<int>w;
vector<pair<int,int> >g[MAX_N],G[MAX_N];
long long edges,cost;
int root;
int a,b;
int parent[MAX_N];
int paredge[MAX_N];
int depth[MAX_N];
vector<int>ENDS;
vector<pair<int,int>>nodes;
bool used[MAX_N];
bool usededge[130010];
void initbfs()
{
    queue<int>q;
    q.push(root);
    used[root]=1;

    while(q.size())
    {
        int u=q.front();
        q.pop();

        for(auto [v,edge]:G[u])
        {
            if(used[v])continue;

            used[v]=1;
            usededge[edge]=1;
            g[u].push_back({v,edge});
            g[v].push_back({u,edge});
            parent[v]=u;
            paredge[v]=edge;
            depth[v]=depth[u]+1;
            q.push(v);
        }
    }
}
int onpath()
{
    vector<pair<int,int>>node;
    for(int i=0;i<n;i++)
    {
        node.push_back({G[i].size(),i});
    }
    sort(node.begin(),node.end());

    int l=0,r=n-1;
    int ans;

    while(l<=r)
    {
        int mid=(l+r)/2;
        for(int u=0;u<=mid;u++)
        {
            for(auto [v,edge]:G[node[u].second])
            {
                w[edge]=1;
            }
        }
        long long ncost=ask(w);q++;
        for(int u=0;u<=mid;u++)
        {
            for(auto [v,edge]:G[node[u].second])
            {
                w[edge]=0;
            }
        }
        if(ncost!=cost)
        {
            ans=node[mid].second;
            r=mid-1;
        }
        else l=mid+1;
    }

    return ans;
}
void nodesdfs(int u)
{
    if(remedges==edges)
    {
        if(depth[u]<=remedges)nodes.push_back({depth[u],u});
    }
    else if(depth[u]==remedges)nodes.push_back({depth[u],u});

    for(auto [v,edge]:g[u])
    {
        if(v==parent[u])continue;

        nodesdfs(v);
    }
}
int rec(int l,int r)
{
    if(l==r)
    {
        return nodes[l].second;
    }

    int mid=(l+r)/2;

    for(int i=l;i<=mid;i++)
    {
        w[paredge[nodes[i].second]]=1;
    }
    long long ncost=ask(w);q++;
    for(int i=l;i<=mid;i++)
    {
        w[paredge[nodes[i].second]]=0;
    }

    if(ncost!=cost)return rec(l,mid);
    return rec(mid+1,r);
}
void solve(int branch,bool with)
{
    nodes.clear();
    for(int i=0;i<g[root].size();i++)
    {
        if(with)
        {
            if(i!=branch)continue;
        }
        else
        {
            if(i==branch)continue;
        }

        nodesdfs(g[root][i].first);
    }
    sort(nodes.rbegin(),nodes.rend());
    ENDS.push_back(rec(0,nodes.size()-1));
}
int solvetwo()
{
    int l=0,r=g[root].size()-1;
    int ans=-1;

    while(l<=r)
    {
        int mid=(l+r)/2;

        for(int i=0;i<=mid;i++)
        {
            w[g[root][i].second]=1;
        }
        long long ncost=ask(w);q++;
        for(int i=0;i<=mid;i++)
        {
            w[g[root][i].second]=0;
        }

        if(ncost!=cost)
        {
            ans=mid;
            r=mid-1;
        }
        else l=mid+1;
    }

    return ans;
}
void find_pair(int N, std::vector<int> U, std::vector<int> V, int A, int B)
{
    n=N;
    m=U.size();
    w.resize(m);
    a=A;
    b=B;

    for(int i=0;i<m;i++)
    {
        G[U[i]].push_back({V[i],i});
        G[V[i]].push_back({U[i],i});
    }

    cost=ask(w);q++;
    edges=cost/a;
    remedges=edges;

    root=onpath();
    initbfs();
    for(int i=0;i<m;i++)
    {
        if(usededge[i])continue;
        w[i]=1;
    }

    int branch=solvetwo();
    solve(branch,1);
    if(depth[ENDS[0]]==edges)
    {
        ENDS.push_back(root);
    }
    else
    {
        remedges-=depth[ENDS[0]];
        solve(branch,0);
    }

    answer(ENDS[0],ENDS[1]);
}

Compilation message

highway.cpp: In function 'void initbfs()':
highway.cpp:34:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   34 |         for(auto [v,edge]:G[u])
      |                  ^
highway.cpp: In function 'int onpath()':
highway.cpp:66:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   66 |             for(auto [v,edge]:G[node[u].second])
      |                      ^
highway.cpp:74:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   74 |             for(auto [v,edge]:G[node[u].second])
      |                      ^
highway.cpp: In function 'void nodesdfs(int)':
highway.cpp:97:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   97 |     for(auto [v,edge]:g[u])
      |              ^
highway.cpp: In function 'void solve(int, bool)':
highway.cpp:129:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |     for(int i=0;i<g[root].size();i++)
      |                 ~^~~~~~~~~~~~~~~
highway.cpp: In function 'int onpath()':
highway.cpp:87:12: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
   87 |     return ans;
      |            ^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4696 KB Output is correct
2 Correct 2 ms 4696 KB Output is correct
3 Correct 2 ms 4696 KB Output is correct
4 Correct 2 ms 4696 KB Output is correct
5 Correct 2 ms 4696 KB Output is correct
6 Correct 2 ms 4696 KB Output is correct
7 Correct 2 ms 4696 KB Output is correct
8 Correct 2 ms 4696 KB Output is correct
9 Correct 2 ms 4696 KB Output is correct
10 Correct 2 ms 4696 KB Output is correct
11 Correct 2 ms 4696 KB Output is correct
12 Correct 2 ms 4696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4696 KB Output is correct
2 Correct 13 ms 5640 KB Output is correct
3 Correct 107 ms 15040 KB Output is correct
4 Correct 128 ms 14340 KB Output is correct
5 Correct 115 ms 15144 KB Output is correct
6 Correct 116 ms 15976 KB Output is correct
7 Correct 99 ms 14656 KB Output is correct
8 Correct 105 ms 14236 KB Output is correct
9 Correct 104 ms 14504 KB Output is correct
10 Correct 104 ms 15312 KB Output is correct
11 Correct 90 ms 14764 KB Output is correct
12 Correct 108 ms 16040 KB Output is correct
13 Correct 96 ms 15252 KB Output is correct
14 Correct 97 ms 14988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 6180 KB Output is correct
2 Correct 20 ms 7468 KB Output is correct
3 Correct 29 ms 7652 KB Output is correct
4 Correct 76 ms 17028 KB Output is correct
5 Correct 78 ms 16732 KB Output is correct
6 Correct 92 ms 18536 KB Output is correct
7 Correct 79 ms 13424 KB Output is correct
8 Correct 83 ms 17596 KB Output is correct
9 Correct 77 ms 14536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4696 KB Output is correct
2 Correct 13 ms 5640 KB Output is correct
3 Correct 78 ms 12080 KB Output is correct
4 Correct 104 ms 14572 KB Output is correct
5 Correct 116 ms 14228 KB Output is correct
6 Correct 112 ms 14236 KB Output is correct
7 Correct 87 ms 14344 KB Output is correct
8 Correct 107 ms 14300 KB Output is correct
9 Correct 111 ms 15272 KB Output is correct
10 Correct 90 ms 15024 KB Output is correct
11 Correct 96 ms 14220 KB Output is correct
12 Correct 99 ms 15784 KB Output is correct
13 Correct 95 ms 14712 KB Output is correct
14 Correct 94 ms 15240 KB Output is correct
15 Correct 94 ms 14304 KB Output is correct
16 Correct 96 ms 14260 KB Output is correct
17 Correct 101 ms 15420 KB Output is correct
18 Correct 91 ms 15436 KB Output is correct
19 Correct 87 ms 14308 KB Output is correct
20 Correct 84 ms 15404 KB Output is correct
21 Correct 101 ms 16788 KB Output is correct
22 Correct 102 ms 16804 KB Output is correct
23 Correct 100 ms 16556 KB Output is correct
24 Correct 114 ms 17088 KB Output is correct
25 Correct 112 ms 20104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 5896 KB Output is correct
2 Correct 14 ms 5896 KB Output is correct
3 Correct 123 ms 15572 KB Output is correct
4 Correct 159 ms 16064 KB Output is correct
5 Correct 152 ms 16372 KB Output is correct
6 Correct 151 ms 17544 KB Output is correct
7 Correct 153 ms 16792 KB Output is correct
8 Correct 155 ms 17708 KB Output is correct
9 Correct 140 ms 12328 KB Output is correct
10 Correct 106 ms 12408 KB Output is correct
11 Correct 128 ms 13856 KB Output is correct
12 Correct 180 ms 14916 KB Output is correct
13 Correct 150 ms 15956 KB Output is correct
14 Correct 155 ms 16680 KB Output is correct
15 Correct 155 ms 16064 KB Output is correct
16 Correct 122 ms 13024 KB Output is correct
17 Correct 99 ms 16816 KB Output is correct
18 Correct 128 ms 17144 KB Output is correct
19 Correct 109 ms 17064 KB Output is correct
20 Correct 105 ms 17156 KB Output is correct
21 Correct 131 ms 16216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 5672 KB Output is correct
2 Correct 13 ms 5896 KB Output is correct
3 Correct 122 ms 15024 KB Output is correct
4 Correct 146 ms 16516 KB Output is correct
5 Correct 147 ms 16060 KB Output is correct
6 Correct 171 ms 16904 KB Output is correct
7 Correct 111 ms 15212 KB Output is correct
8 Correct 128 ms 15136 KB Output is correct
9 Correct 123 ms 15336 KB Output is correct
10 Correct 158 ms 16664 KB Output is correct
11 Correct 141 ms 16716 KB Output is correct
12 Correct 144 ms 16364 KB Output is correct
13 Correct 108 ms 13584 KB Output is correct
14 Correct 107 ms 13264 KB Output is correct
15 Correct 114 ms 13532 KB Output is correct
16 Correct 101 ms 12968 KB Output is correct
17 Correct 116 ms 14424 KB Output is correct
18 Correct 104 ms 13236 KB Output is correct
19 Correct 154 ms 15692 KB Output is correct
20 Correct 126 ms 15412 KB Output is correct
21 Correct 136 ms 16100 KB Output is correct
22 Correct 156 ms 16424 KB Output is correct
23 Correct 140 ms 16532 KB Output is correct
24 Correct 134 ms 16048 KB Output is correct
25 Correct 147 ms 16448 KB Output is correct
26 Correct 158 ms 16512 KB Output is correct
27 Correct 103 ms 17088 KB Output is correct
28 Correct 99 ms 16876 KB Output is correct
29 Correct 114 ms 17844 KB Output is correct
30 Correct 101 ms 17092 KB Output is correct
31 Correct 116 ms 17052 KB Output is correct
32 Correct 111 ms 17016 KB Output is correct
33 Correct 96 ms 17288 KB Output is correct
34 Correct 95 ms 17080 KB Output is correct
35 Correct 97 ms 16988 KB Output is correct
36 Correct 109 ms 16856 KB Output is correct
37 Correct 112 ms 17324 KB Output is correct
38 Correct 110 ms 17040 KB Output is correct
39 Correct 145 ms 16752 KB Output is correct
40 Correct 128 ms 16840 KB Output is correct
41 Correct 145 ms 16456 KB Output is correct
42 Correct 144 ms 16160 KB Output is correct