Submission #292547

# Submission time Handle Problem Language Result Execution time Memory
292547 2020-09-07T09:04:52 Z daniel920712 Swapping Cities (APIO20_swap) C++14
37 / 100
2000 ms 31468 KB
#include "swap.h"
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <utility>
using namespace std;
int NN;
int MM;
int all[200005];
bool use[200005];
bool use2[200005];
bool have2[200005];
bool have3[200005];
int Father[200005];
int y;
int ans=0;
int how=0;
vector < pair < pair < int , int > , int > > Next[200005];
vector < pair < pair < int , int > , int > > Next2[200005];
priority_queue < pair < int , int > , vector < pair < int , int > > , greater < pair < int , int >  > > dd;
struct A
{
    int u,v,w;
    int con;
}edge[200005];
vector < int > have;
bool cmp(pair < pair < int , int > , int > a,pair < pair < int , int > , int > b)
{
    return a.first.second<b.first.second;
}
bool cmp2(A a,A b)
{
    return a.w<b.w;
}
bool F(int here,int fa)
{
    if(here==y)
    {
        //printf("%d\n",here);
        have.push_back(here);
        have2[here]=1;
        return 1;
    }
    for(auto i:Next2[here])
    {
        if(i.first.first==fa) continue;
        if(F(i.first.first,here))
        {
            //printf("%d\n",here);
            ans=max(ans,i.first.second);
            use[i.second]=1;
            have.push_back(here);
            have2[here]=1;
            return 1;

        }
    }
    return 0;
}
int Find(int here)
{
    if(Father[here]==here) return here;
    Father[here]=Find(Father[here]);
    return Father[here];
}
void init(int N, int M,vector<int> U, vector<int> V, vector<int> W)
{
    NN=N;
    MM=M;
    int i;
    for(i=0;i<M;i++)
    {
        Next[U[i]].push_back(make_pair(make_pair(V[i],W[i]),i));
        Next[V[i]].push_back(make_pair(make_pair(U[i],W[i]),i));
        edge[i].u=U[i];
        edge[i].v=V[i];
        edge[i].w=W[i];
        edge[i].con=i;
    }
    for(i=0;i<N;i++)
    {
        Father[i]=i;
        sort(Next[i].begin(),Next[i].end(),cmp);
    }
    sort(edge,edge+M,cmp2);
    for(i=0;i<M;i++)
    {
        if(Find(edge[i].u)==Find(edge[i].v)) continue;
        Father[Find(edge[i].u)]=Find(edge[i].v);
        Next2[edge[i].u].push_back(make_pair(make_pair(edge[i].v,edge[i].w),edge[i].con));
        Next2[edge[i].v].push_back(make_pair(make_pair(edge[i].u,edge[i].w),edge[i].con));
    }
}

int getMinimumFuelCapacity(int x, int y)
{
    int t=2e9,t2=-1,con=0,i,j,a,b;
    ::y=y;
    for(i=0;i<MM;i++) use[i]=0;
    for(i=0;i<=NN;i++) have2[i]=0;
    ans=0;
    have.clear();
    F(x,-1);
    for(auto i:have)
    {
        con=0;
        t2=-1;
        for(auto j:Next[i])
        {
            if(use[j.second]) continue;
            if(i==x||i==y) continue;
            con++;

            if((i!=x&&i!=y&&con>=1)||con>=2)
            {
                //printf("%d %d\n",i,j.first.second);
                t2=j.first.second;
                break;
            }


        }
        if(t2!=-1) t=min(t,t2);
    }

    for(auto i:Next[x])
    {
        if(use[i.second]) continue;
        for(j=0;j<=NN;j++) use2[j]=0;
        for(j=0;j<=MM;j++) have3[j]=0;
        have3[i.second]=1;
        dd.push(make_pair(i.first.second,i.first.first));
        while(!dd.empty())
        {
            a=dd.top().first;
            b=dd.top().second;
            dd.pop();
            if(use2[b])
            {
                t=min(t,a);
                break;
            }
            if(have2[b])
            {
                t=min(t,a);
                break;
            }
            use2[b]=1;
            con=0;
            for(auto j:Next[b])
            {
                if(have3[j.second]||use[j.second]) continue;
                con++;
                if(con==2) t=min(t,max(j.first.second,a));
                have3[j.second]=1;
                dd.push(make_pair(max(j.first.second,a),j.first.first));

            }
        }
        while(!dd.empty()) dd.pop();
    }
    for(auto i:Next[y])
    {
        if(use[i.second]) continue;
        for(j=0;j<=NN;j++) use2[j]=0;
        for(j=0;j<=MM;j++) have3[j]=0;
        have3[i.second]=1;
        dd.push(make_pair(i.first.second,i.first.first));
        while(!dd.empty())
        {
            a=dd.top().first;
            b=dd.top().second;
            dd.pop();
            if(use2[b])
            {
                t=min(t,a);
                break;
            }
            if(have2[b])
            {
                t=min(t,a);
                break;
            }
            use2[b]=1;
            con=0;
            for(auto j:Next[b])
            {
                if(have3[j.second]||use[j.second]) continue;
                have3[j.second]=1;
                con++;
                if(con==2) t=min(t,max(j.first.second,a));
                dd.push(make_pair(max(j.first.second,a),j.first.first));
            }
        }
        while(!dd.empty()) dd.pop();
    }
    if(t>=2000000000) return -1;
    return max(t,ans);
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 6 ms 9728 KB Output is correct
3 Correct 6 ms 9728 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 8 ms 9856 KB Output is correct
6 Correct 7 ms 9856 KB Output is correct
7 Correct 8 ms 9856 KB Output is correct
8 Correct 8 ms 9856 KB Output is correct
9 Correct 125 ms 20440 KB Output is correct
10 Correct 196 ms 24460 KB Output is correct
11 Correct 219 ms 24860 KB Output is correct
12 Correct 228 ms 25568 KB Output is correct
13 Correct 191 ms 25180 KB Output is correct
14 Execution timed out 2084 ms 22748 KB Time limit exceeded
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 6 ms 9728 KB Output is correct
3 Execution timed out 2079 ms 23468 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 6 ms 9728 KB Output is correct
4 Correct 6 ms 9728 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 8 ms 9856 KB Output is correct
7 Correct 7 ms 9856 KB Output is correct
8 Correct 8 ms 9856 KB Output is correct
9 Correct 8 ms 9856 KB Output is correct
10 Correct 7 ms 9856 KB Output is correct
11 Correct 8 ms 9856 KB Output is correct
12 Correct 7 ms 9856 KB Output is correct
13 Correct 7 ms 9856 KB Output is correct
14 Correct 7 ms 9856 KB Output is correct
15 Correct 8 ms 9856 KB Output is correct
16 Correct 8 ms 9856 KB Output is correct
17 Correct 7 ms 9856 KB Output is correct
18 Correct 7 ms 9856 KB Output is correct
19 Correct 8 ms 9856 KB Output is correct
20 Correct 8 ms 9856 KB Output is correct
21 Correct 7 ms 9856 KB Output is correct
22 Correct 10 ms 9856 KB Output is correct
23 Correct 7 ms 9856 KB Output is correct
24 Correct 9 ms 9856 KB Output is correct
25 Correct 9 ms 9984 KB Output is correct
26 Correct 9 ms 9984 KB Output is correct
27 Correct 7 ms 9856 KB Output is correct
28 Correct 8 ms 9856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 6 ms 9728 KB Output is correct
4 Correct 6 ms 9728 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 8 ms 9856 KB Output is correct
7 Correct 7 ms 9856 KB Output is correct
8 Correct 8 ms 9856 KB Output is correct
9 Correct 8 ms 9856 KB Output is correct
10 Correct 125 ms 20440 KB Output is correct
11 Correct 196 ms 24460 KB Output is correct
12 Correct 219 ms 24860 KB Output is correct
13 Correct 228 ms 25568 KB Output is correct
14 Correct 191 ms 25180 KB Output is correct
15 Correct 7 ms 9856 KB Output is correct
16 Correct 8 ms 9856 KB Output is correct
17 Correct 7 ms 9856 KB Output is correct
18 Correct 7 ms 9856 KB Output is correct
19 Correct 7 ms 9856 KB Output is correct
20 Correct 8 ms 9856 KB Output is correct
21 Correct 8 ms 9856 KB Output is correct
22 Correct 7 ms 9856 KB Output is correct
23 Correct 7 ms 9856 KB Output is correct
24 Correct 20 ms 11520 KB Output is correct
25 Correct 213 ms 23520 KB Output is correct
26 Correct 220 ms 22556 KB Output is correct
27 Correct 162 ms 20448 KB Output is correct
28 Correct 138 ms 20216 KB Output is correct
29 Correct 160 ms 20344 KB Output is correct
30 Correct 122 ms 20088 KB Output is correct
31 Correct 244 ms 23236 KB Output is correct
32 Correct 196 ms 22496 KB Output is correct
33 Correct 179 ms 23008 KB Output is correct
34 Correct 170 ms 22136 KB Output is correct
35 Correct 8 ms 9856 KB Output is correct
36 Correct 8 ms 9856 KB Output is correct
37 Correct 7 ms 9856 KB Output is correct
38 Correct 10 ms 9856 KB Output is correct
39 Correct 7 ms 9856 KB Output is correct
40 Correct 9 ms 9856 KB Output is correct
41 Correct 9 ms 9984 KB Output is correct
42 Correct 9 ms 9984 KB Output is correct
43 Correct 7 ms 9856 KB Output is correct
44 Correct 8 ms 9856 KB Output is correct
45 Correct 208 ms 24436 KB Output is correct
46 Correct 202 ms 21468 KB Output is correct
47 Correct 175 ms 20472 KB Output is correct
48 Correct 162 ms 21496 KB Output is correct
49 Correct 1397 ms 23036 KB Output is correct
50 Correct 230 ms 19448 KB Output is correct
51 Correct 202 ms 22460 KB Output is correct
52 Correct 295 ms 26896 KB Output is correct
53 Correct 250 ms 28280 KB Output is correct
54 Correct 486 ms 31468 KB Output is correct
55 Correct 143 ms 25184 KB Output is correct
56 Correct 275 ms 27280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 6 ms 9728 KB Output is correct
3 Correct 6 ms 9728 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 8 ms 9856 KB Output is correct
6 Correct 7 ms 9856 KB Output is correct
7 Correct 8 ms 9856 KB Output is correct
8 Correct 8 ms 9856 KB Output is correct
9 Correct 125 ms 20440 KB Output is correct
10 Correct 196 ms 24460 KB Output is correct
11 Correct 219 ms 24860 KB Output is correct
12 Correct 228 ms 25568 KB Output is correct
13 Correct 191 ms 25180 KB Output is correct
14 Execution timed out 2084 ms 22748 KB Time limit exceeded
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 6 ms 9728 KB Output is correct
4 Correct 6 ms 9728 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 8 ms 9856 KB Output is correct
7 Correct 7 ms 9856 KB Output is correct
8 Correct 8 ms 9856 KB Output is correct
9 Correct 8 ms 9856 KB Output is correct
10 Correct 125 ms 20440 KB Output is correct
11 Correct 196 ms 24460 KB Output is correct
12 Correct 219 ms 24860 KB Output is correct
13 Correct 228 ms 25568 KB Output is correct
14 Correct 191 ms 25180 KB Output is correct
15 Execution timed out 2084 ms 22748 KB Time limit exceeded
16 Halted 0 ms 0 KB -