답안 #1101594

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1101594 2024-10-16T11:18:26 Z MMihalev Simurgh (IOI17_simurgh) C++14
70 / 100
95 ms 5804 KB
#include<iostream>
#include<algorithm>
#include<set>
#include<queue>
#include "simurgh.h"
using namespace std;
const int MAX_N=5e2+3;
const int MAX_M=125000;
int n,m;
int q;
vector<int>edges;
vector<pair<int,int>>g[MAX_N];
bool used[MAX_N];
vector<int>comp;
vector<vector<int>>comps;
int adj[MAX_N];
int numedge[MAX_N][MAX_N];

bool stat[MAX_M];
bool checked[MAX_M];
int forsure;


bool mm[MAX_N];
bool uu[MAX_N];
int pre[MAX_N];
void reset()
{
    comps.clear();
    edges.clear();
    for(int i=0;i<n;i++)
    {
        adj[i]=-1;
        used[i]=0;
    }
}
void dfs(int u)
{
    if(adj[u]!=-1)
    {
        if(!checked[adj[u]])
        {
            comp.push_back(adj[u]);
        }
        if(stat[adj[u]])forsure=adj[u];
    }
    used[u]=1;
    for(auto [v,edge]:g[u])
    {
        if(used[v])continue;
        edges.push_back(edge);
        dfs(v);
    }
}
void rep(int prev,int edge)
{
    for(int i=0;i<edges.size();i++)
    {
        if(edges[i]==prev){edges[i]=edge;return;}
    }
}
vector<pair<int,int>>s;
int rec(int l,int r)
{
    if(l==r)
    {
        return s[l].first;
    }

    int mid=(l+r)/2;

    int bonus=0;
    edges.clear();
    for(int i=0;i<n;i++)used[i]=0;

    for(int i=l;i<=mid;i++)
    {
        edges.push_back(s[i].first);
        used[s[i].second]=1;
    }

    for(int i=1;i<n;i++)
    {
        if(!used[i]){bonus+=stat[numedge[0][i]];edges.push_back(numedge[0][i]);}
    }

    int cnt=count_common_roads(edges)-bonus;q++;

    if(cnt>0)return rec(l,mid);
    return rec(mid+1,r);
}

bool dfu[MAX_N];
int vers;
void df(int u)
{
    vers++;
    dfu[u]=1;
    for(auto [v,edge]:g[u])
    {
        if(dfu[v] or mm[v]==0)continue;
        df(v);
    }
}
vector<int> find_roads(int N, vector<int> u, vector<int> v)
{
    n=N;
    m=u.size();

    vector<int>ans;
    for(int i=0;i<m;i++)
    {
        g[u[i]].push_back({v[i],i});
        g[v[i]].push_back({u[i],i});
        numedge[u[i]][v[i]]=i;
        numedge[v[i]][u[i]]=i;
    }

    set<pair<int,int>>so;
    int e=0;
    for(int i=0;i<n;i++)
    {
        so.insert({g[i].size(),i});
        pre[i]=g[i].size();
    }

    int cc=0;
    vector<int>verts;
    while(so.size()>0)
    {
        auto f=(*(so.rbegin()));
        so.erase(f);
        int u=f.second;
        verts.push_back(u);
        e+=g[u].size();

        cc++;
        mm[u]=1;
        for(auto [v,edge]:g[u])uu[v]=1;

        for(int i=0;i<n;i++)
        {
            if(mm[i])continue;
            int npre=0;
            for(auto [v,edge]:g[i])
            {
                if(uu[v] or mm[v])continue;

                npre++;
            }
            so.erase({pre[i],i});
            pre[i]=npre;
            so.insert({pre[i],i});
        }

        bool bad=0;
        for(int i=0;i<n;i++)
        {
            if(mm[i])continue;
            if(uu[i])continue;
            bad=1;
            break;
        }
        if(bad==0)
        {
            break;
        }
    }

    int tt=50;
    vector<pair<int,int>>pazo;
    for(int i=0;i<n;i++)
    {
        pazo.push_back({g[i].size(),i});
    }
    sort(pazo.rbegin(),pazo.rend());

    for(auto [sss,i]:pazo)
    {
        if(mm[i])continue;
        if(tt>0)
        {
            e+=g[i].size();
            mm[i]=1;
            verts.push_back(i);
            tt--;
        }
    }

    df(verts[0]);
    if(vers!=verts.size())while(1);


    if(m!=n*(n-1)/2)
    {
        for(int u=0;u<n;u++)
        {
            reset();
            used[u]=1;
            for(auto [v,edge]:g[u])
            {
                adj[v]=edge;
            }
            for(auto [v,edge]:g[u])
            {
                if(!used[v])
                {
                    forsure=-1;
                    comp.clear();
                    dfs(v);
                    if(forsure!=-1)
                    {
                        comp.push_back(forsure);
                    }
                    comps.push_back(comp);
                }
            }

            for(auto c:comps)
            {
                edges.push_back(c[0]);
            }

            for(auto c:comps)
            {
                int ma=count_common_roads(edges);q++;
                vector<int>marked;
                marked.push_back(c[0]);
                int prev=c[0];
                int sega=0;

                for(int edge:c)
                {
                    if(edge==c[0])continue;

                    rep(prev,edge);

                    int cur=count_common_roads(edges);q++;
                    if(cur>ma)
                    {
                        sega++;
                        marked.clear();
                        ma=cur;
                        marked.push_back(edge);
                        if(sega==2)break;
                    }
                    else if(cur==ma)
                    {
                        marked.push_back(edge);
                    }

                    prev=edge;
                }

                for(int edge:marked)stat[edge]=1;
            }

            for(auto [v,edge]:g[u])checked[edge]=1;
        }

    }
    else
    {
        for(int u=0;u<n;u++)
        {
            if(u==0)
            {
                vector<int>marked;
                edges.clear();
                for(int v=2;v<n;v++)
                {
                    edges.push_back(numedge[v-1][v]);
                }

                int ma=-1;

                for(auto [v,edge]:g[u])
                {
                    edges.push_back(edge);
                    int cur=count_common_roads(edges);q++;
                    edges.pop_back();

                    if(cur>ma)
                    {
                        marked.clear();
                        marked.push_back(edge);
                        ma=cur;
                    }
                    else if(cur==ma)marked.push_back(edge);
                }

                for(int edge:marked)stat[edge]=1;

                continue;
            }


            while(1)
            {
                int bonus=0;
                edges.clear();
                s.clear();
                for(int i=0;i<n;i++)used[i]=0;

                for(auto [v,edge]:g[u])
                {
                    if(stat[edge] or v==0)continue;
                    edges.push_back(edge);
                    s.push_back({edge,v});
                    used[v]=1;
                }

                for(int i=1;i<n;i++)
                {
                    if(!used[i]){bonus+=stat[numedge[0][i]];edges.push_back(numedge[0][i]);}
                }

                int cur=count_common_roads(edges)-bonus;q++;

                if(cur==0)break;

                int edge=rec(0,s.size()-1);
                stat[edge]=1;
            }
        }

    }

    for(int i=0;i<m;i++)
    {
        if(stat[i])ans.push_back(i);
    }

    return ans;
}

Compilation message

simurgh.cpp: In function 'void dfs(int)':
simurgh.cpp:48:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   48 |     for(auto [v,edge]:g[u])
      |              ^
simurgh.cpp: In function 'void rep(int, int)':
simurgh.cpp:57:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |     for(int i=0;i<edges.size();i++)
      |                 ~^~~~~~~~~~~~~
simurgh.cpp: In function 'void df(int)':
simurgh.cpp:99:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   99 |     for(auto [v,edge]:g[u])
      |              ^
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:139:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  139 |         for(auto [v,edge]:g[u])uu[v]=1;
      |                  ^
simurgh.cpp:145:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  145 |             for(auto [v,edge]:g[i])
      |                      ^
simurgh.cpp:178:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  178 |     for(auto [sss,i]:pazo)
      |              ^
simurgh.cpp:191:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  191 |     if(vers!=verts.size())while(1);
      |        ~~~~^~~~~~~~~~~~~~
simurgh.cpp:200:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  200 |             for(auto [v,edge]:g[u])
      |                      ^
simurgh.cpp:204:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  204 |             for(auto [v,edge]:g[u])
      |                      ^
simurgh.cpp:258:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  258 |             for(auto [v,edge]:g[u])checked[edge]=1;
      |                      ^
simurgh.cpp:277:26: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  277 |                 for(auto [v,edge]:g[u])
      |                          ^
simurgh.cpp:305:26: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  305 |                 for(auto [v,edge]:g[u])
      |                          ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB correct
2 Correct 1 ms 336 KB correct
3 Correct 1 ms 336 KB correct
4 Correct 1 ms 336 KB correct
5 Correct 1 ms 336 KB correct
6 Correct 1 ms 336 KB correct
7 Correct 1 ms 336 KB correct
8 Correct 1 ms 336 KB correct
9 Correct 1 ms 336 KB correct
10 Correct 1 ms 336 KB correct
11 Correct 1 ms 504 KB correct
12 Correct 1 ms 336 KB correct
13 Correct 1 ms 336 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB correct
2 Correct 1 ms 336 KB correct
3 Correct 1 ms 336 KB correct
4 Correct 1 ms 336 KB correct
5 Correct 1 ms 336 KB correct
6 Correct 1 ms 336 KB correct
7 Correct 1 ms 336 KB correct
8 Correct 1 ms 336 KB correct
9 Correct 1 ms 336 KB correct
10 Correct 1 ms 336 KB correct
11 Correct 1 ms 504 KB correct
12 Correct 1 ms 336 KB correct
13 Correct 1 ms 336 KB correct
14 Correct 1 ms 592 KB correct
15 Correct 1 ms 592 KB correct
16 Correct 1 ms 592 KB correct
17 Correct 2 ms 760 KB correct
18 Correct 1 ms 592 KB correct
19 Correct 2 ms 592 KB correct
20 Correct 2 ms 596 KB correct
21 Correct 2 ms 592 KB correct
22 Correct 1 ms 592 KB correct
23 Correct 2 ms 592 KB correct
24 Correct 2 ms 592 KB correct
25 Correct 1 ms 336 KB correct
26 Correct 1 ms 592 KB correct
27 Correct 2 ms 764 KB correct
28 Correct 1 ms 336 KB correct
29 Correct 1 ms 504 KB correct
30 Correct 1 ms 592 KB correct
31 Correct 1 ms 592 KB correct
32 Correct 1 ms 592 KB correct
33 Correct 1 ms 592 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB correct
2 Correct 1 ms 336 KB correct
3 Correct 1 ms 336 KB correct
4 Correct 1 ms 336 KB correct
5 Correct 1 ms 336 KB correct
6 Correct 1 ms 336 KB correct
7 Correct 1 ms 336 KB correct
8 Correct 1 ms 336 KB correct
9 Correct 1 ms 336 KB correct
10 Correct 1 ms 336 KB correct
11 Correct 1 ms 504 KB correct
12 Correct 1 ms 336 KB correct
13 Correct 1 ms 336 KB correct
14 Correct 1 ms 592 KB correct
15 Correct 1 ms 592 KB correct
16 Correct 1 ms 592 KB correct
17 Correct 2 ms 760 KB correct
18 Correct 1 ms 592 KB correct
19 Correct 2 ms 592 KB correct
20 Correct 2 ms 596 KB correct
21 Correct 2 ms 592 KB correct
22 Correct 1 ms 592 KB correct
23 Correct 2 ms 592 KB correct
24 Correct 2 ms 592 KB correct
25 Correct 1 ms 336 KB correct
26 Correct 1 ms 592 KB correct
27 Correct 2 ms 764 KB correct
28 Correct 1 ms 336 KB correct
29 Correct 1 ms 504 KB correct
30 Correct 1 ms 592 KB correct
31 Correct 1 ms 592 KB correct
32 Correct 1 ms 592 KB correct
33 Correct 1 ms 592 KB correct
34 Correct 14 ms 1872 KB correct
35 Correct 88 ms 2040 KB correct
36 Correct 61 ms 1616 KB correct
37 Correct 8 ms 848 KB correct
38 Correct 95 ms 2028 KB correct
39 Correct 76 ms 1872 KB correct
40 Correct 66 ms 1616 KB correct
41 Correct 16 ms 1872 KB correct
42 Correct 86 ms 2196 KB correct
43 Correct 50 ms 1616 KB correct
44 Correct 40 ms 1360 KB correct
45 Correct 46 ms 1360 KB correct
46 Correct 36 ms 1360 KB correct
47 Correct 17 ms 1104 KB correct
48 Correct 5 ms 848 KB correct
49 Correct 7 ms 1028 KB correct
50 Correct 18 ms 1180 KB correct
51 Correct 46 ms 1360 KB correct
52 Correct 40 ms 1428 KB correct
53 Correct 39 ms 1360 KB correct
54 Correct 50 ms 1616 KB correct
55 Correct 45 ms 1360 KB correct
56 Correct 47 ms 1360 KB correct
57 Correct 53 ms 1508 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB correct
2 Correct 1 ms 336 KB correct
3 Correct 40 ms 4424 KB correct
4 Correct 61 ms 5804 KB correct
5 Correct 73 ms 5676 KB correct
6 Correct 60 ms 5712 KB correct
7 Correct 57 ms 5712 KB correct
8 Correct 58 ms 5712 KB correct
9 Correct 65 ms 5712 KB correct
10 Correct 70 ms 5712 KB correct
11 Correct 61 ms 5600 KB correct
12 Correct 61 ms 5712 KB correct
13 Correct 1 ms 336 KB correct
14 Correct 62 ms 5688 KB correct
15 Correct 62 ms 5712 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB correct
2 Correct 1 ms 336 KB correct
3 Correct 1 ms 336 KB correct
4 Correct 1 ms 336 KB correct
5 Correct 1 ms 336 KB correct
6 Correct 1 ms 336 KB correct
7 Correct 1 ms 336 KB correct
8 Correct 1 ms 336 KB correct
9 Correct 1 ms 336 KB correct
10 Correct 1 ms 336 KB correct
11 Correct 1 ms 504 KB correct
12 Correct 1 ms 336 KB correct
13 Correct 1 ms 336 KB correct
14 Correct 1 ms 592 KB correct
15 Correct 1 ms 592 KB correct
16 Correct 1 ms 592 KB correct
17 Correct 2 ms 760 KB correct
18 Correct 1 ms 592 KB correct
19 Correct 2 ms 592 KB correct
20 Correct 2 ms 596 KB correct
21 Correct 2 ms 592 KB correct
22 Correct 1 ms 592 KB correct
23 Correct 2 ms 592 KB correct
24 Correct 2 ms 592 KB correct
25 Correct 1 ms 336 KB correct
26 Correct 1 ms 592 KB correct
27 Correct 2 ms 764 KB correct
28 Correct 1 ms 336 KB correct
29 Correct 1 ms 504 KB correct
30 Correct 1 ms 592 KB correct
31 Correct 1 ms 592 KB correct
32 Correct 1 ms 592 KB correct
33 Correct 1 ms 592 KB correct
34 Correct 14 ms 1872 KB correct
35 Correct 88 ms 2040 KB correct
36 Correct 61 ms 1616 KB correct
37 Correct 8 ms 848 KB correct
38 Correct 95 ms 2028 KB correct
39 Correct 76 ms 1872 KB correct
40 Correct 66 ms 1616 KB correct
41 Correct 16 ms 1872 KB correct
42 Correct 86 ms 2196 KB correct
43 Correct 50 ms 1616 KB correct
44 Correct 40 ms 1360 KB correct
45 Correct 46 ms 1360 KB correct
46 Correct 36 ms 1360 KB correct
47 Correct 17 ms 1104 KB correct
48 Correct 5 ms 848 KB correct
49 Correct 7 ms 1028 KB correct
50 Correct 18 ms 1180 KB correct
51 Correct 46 ms 1360 KB correct
52 Correct 40 ms 1428 KB correct
53 Correct 39 ms 1360 KB correct
54 Correct 50 ms 1616 KB correct
55 Correct 45 ms 1360 KB correct
56 Correct 47 ms 1360 KB correct
57 Correct 53 ms 1508 KB correct
58 Correct 1 ms 336 KB correct
59 Correct 1 ms 336 KB correct
60 Correct 40 ms 4424 KB correct
61 Correct 61 ms 5804 KB correct
62 Correct 73 ms 5676 KB correct
63 Correct 60 ms 5712 KB correct
64 Correct 57 ms 5712 KB correct
65 Correct 58 ms 5712 KB correct
66 Correct 65 ms 5712 KB correct
67 Correct 70 ms 5712 KB correct
68 Correct 61 ms 5600 KB correct
69 Correct 61 ms 5712 KB correct
70 Correct 1 ms 336 KB correct
71 Correct 62 ms 5688 KB correct
72 Correct 62 ms 5712 KB correct
73 Correct 1 ms 336 KB correct
74 Correct 65 ms 5788 KB correct
75 Incorrect 64 ms 5756 KB WA in grader: NO
76 Halted 0 ms 0 KB -