Submission #1172836


Source Code Expand

using System;
using System.Text;
using System.Collections.Generic;
class Solve{
    int N;
    List<int>[] G;
    int A,B;
    bool[] even;
    bool[] b;
    int ce,co;
    long ans;
    public Solve(){}
    StringBuilder sb;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        string[] str = Console.ReadLine().Split(' ');
        N = int.Parse(str[0]);
        int M = int.Parse(str[1]);
        G = new List<int>[N];
        for(int i=0;i<N;i++){
            G[i] = new List<int>();
        }
        if(M == N-1){
            for(int i=0;i<M;i++){
                str = Console.ReadLine().Split(' ');
                int x = int.Parse(str[0])-1;
                int y = int.Parse(str[1])-1;
                G[x].Add(y);
                G[y].Add(x);
            }
        }
        else{
            UnionFind U = new UnionFind(N);
            for(int i=0;i<M;i++){
                str = Console.ReadLine().Split(' ');
                int x = int.Parse(str[0])-1;
                int y = int.Parse(str[1])-1;
                if(!U.Same(x,y)){
                    G[x].Add(y);
                    G[y].Add(x);
                    U.Union(x,y);
                }
                else{
                    A = x;
                    B = y;
                }
            }
        }
        even = new bool[N];
        b = new bool[N];
        ce = 0;
        co = 0;
        checkodd(0,true);
        if(M == N-1){
            if(ce != co){
                sb.Append("-1\n");
            }
            else{
                ans = 0;
                b = new bool[N];
                dfs(0);
                sb.Append(ans+"\n");
            }
        }
        else{
            if(even[A] != even[B]){
                if(ce != co){
                    sb.Append("-1\n");
                }
                else{
                    long bf = -N;
                    long bl = N;
                    while(bl - bf >= 3){
                        long bc1 = (2*bf+bl)/3 - ((2*bf+bl)%3 < 0 ? 1 : 0);
                        long bc2 = (bf+2*bl)/3 - ((bf+2*bl)%3 < 0 ? 1 : 0);
                        long a1 = check(bc1);
                        long a2 = check(bc2); 
                        if(a1 >= a2){
                            bf = bc1;
                        }
                        if(a1 <= a2){
                            bl = bc2;
                        }
                    }
                    for(long i=bf;i<=bl;i++){
                        bf = check(bf) < check(i) ? bf : i;
                    }
                    sb.Append(check(bf)+"\n");
                }
            }
            else{
                if(N % 2 == 1){
                    sb.Append("-1\n");
                }
                else{
                    ans = 0;
                    b = new bool[N];
                    dfs2(A,(ce-co)/2);
                    sb.Append(ans+"\n");
                }
            }
        }
    }
    long check(long ex){
        b = new bool[N];
        ans = 0;
        dfs2(A,ex);
        return ans;
    }
    long dfs2(int v,long ex){
        b[v] = true;
        long count = 0;
        if(v == B){
            count -= ex;
        }
        for(int i=0;i<G[v].Count;i++){
            int t = G[v][i];
            if(!b[t]){
                count += dfs2(t,ex);
            }
        }
        if(even[v]){
            count++;
        }
        else{
            count--;
        }
        ans += Math.Max(count,-count);
        return count;
    }
    long dfs(int v){
        b[v] = true;
        long count = 0;
        for(int i=0;i<G[v].Count;i++){
            int t = G[v][i];
            if(!b[t]){
                count += dfs(t);
            }
        }
        if(even[v]){
            count++;
        }
        else{
            count--;
        }
        ans += Math.Max(count,-count);
        return count;
    }
    void checkodd(int v,bool e){
        even[v] = e;
        b[v] = true;
        if(e){
            ce++;
        }
        else{
            co++;
        }
        for(int i=0;i<G[v].Count;i++){
            int t = G[v][i];
            if(!b[t]){
                checkodd(t,!e);
            }
        }
    }
}
class UnionFind{
    int[] par;
    public UnionFind(int n){
        par = new int[n];
        for(int i=0;i<n;i++){
            par[i] = i;
        }
    }
    public void Union(int x,int y){
        par[Get(x)] = Get(y); 
    }
    public bool Same(int x,int y){
        return Get(x) == Get(y);
    }
    int Get(int x){
        if(x != par[x]){
            par[x] = Get(par[x]);
        }
        return par[x];
    }
}

Submission Info

Submission Time
Task F - Namori
User leign
Language C# (Mono 4.6.2.0)
Score 2200
Code Size 4951 Byte
Status AC
Exec Time 1126 ms
Memory 37512 KB

Judge Result

Set Name Sample Subtask1 All
Score / Max Score 0 / 0 1500 / 1500 700 / 700
Status
AC × 4
AC × 20
AC × 66
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt
Subtask1 0_00.txt, 0_01.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 2_00.txt, 2_01.txt, 2_02.txt, 2_03.txt, 2_04.txt, 2_05.txt, 2_06.txt, 2_07.txt, 2_08.txt, 2_09.txt, 2_10.txt, 2_11.txt, 2_12.txt, 2_13.txt, 2_14.txt, 2_15.txt, 2_16.txt, 2_17.txt, 2_18.txt, 2_19.txt, 2_20.txt, 2_21.txt, 2_22.txt, 2_23.txt, 2_24.txt, 2_25.txt, 2_26.txt, 2_27.txt, 2_28.txt, 2_29.txt, 2_30.txt, 2_31.txt, 2_32.txt, 2_33.txt, 2_34.txt, 2_35.txt, 2_36.txt, 2_37.txt, 2_38.txt, 2_39.txt, 2_40.txt, 2_41.txt, 2_42.txt, 2_43.txt
Case Name Status Exec Time Memory
0_00.txt AC 22 ms 11220 KB
0_01.txt AC 21 ms 11220 KB
0_02.txt AC 22 ms 9172 KB
0_03.txt AC 22 ms 11220 KB
1_00.txt AC 22 ms 11220 KB
1_01.txt AC 180 ms 27404 KB
1_02.txt AC 167 ms 24460 KB
1_03.txt AC 159 ms 23564 KB
1_04.txt AC 178 ms 27656 KB
1_05.txt AC 188 ms 26764 KB
1_06.txt AC 176 ms 26636 KB
1_07.txt AC 179 ms 26380 KB
1_08.txt AC 177 ms 25100 KB
1_09.txt AC 169 ms 25992 KB
1_10.txt AC 170 ms 25096 KB
1_11.txt AC 170 ms 24840 KB
1_12.txt AC 180 ms 24844 KB
1_13.txt AC 164 ms 25100 KB
1_14.txt AC 167 ms 24716 KB
1_15.txt AC 178 ms 22796 KB
1_16.txt AC 164 ms 22668 KB
1_17.txt AC 179 ms 28940 KB
2_00.txt AC 22 ms 9172 KB
2_01.txt AC 1019 ms 37004 KB
2_02.txt AC 492 ms 32012 KB
2_03.txt AC 347 ms 29448 KB
2_04.txt AC 978 ms 34316 KB
2_05.txt AC 1126 ms 34060 KB
2_06.txt AC 1037 ms 36104 KB
2_07.txt AC 1054 ms 34316 KB
2_08.txt AC 1015 ms 34184 KB
2_09.txt AC 807 ms 34824 KB
2_10.txt AC 543 ms 32780 KB
2_11.txt AC 842 ms 33164 KB
2_12.txt AC 790 ms 33032 KB
2_13.txt AC 176 ms 29192 KB
2_14.txt AC 168 ms 25228 KB
2_15.txt AC 815 ms 31116 KB
2_16.txt AC 181 ms 29196 KB
2_17.txt AC 804 ms 33036 KB
2_18.txt AC 22 ms 11220 KB
2_19.txt AC 181 ms 28684 KB
2_20.txt AC 161 ms 24328 KB
2_21.txt AC 972 ms 37388 KB
2_22.txt AC 186 ms 29192 KB
2_23.txt AC 180 ms 28556 KB
2_24.txt AC 184 ms 28300 KB
2_25.txt AC 182 ms 27144 KB
2_26.txt AC 182 ms 28300 KB
2_27.txt AC 183 ms 28548 KB
2_28.txt AC 186 ms 24068 KB
2_29.txt AC 175 ms 25484 KB
2_30.txt AC 179 ms 25228 KB
2_31.txt AC 169 ms 28044 KB
2_32.txt AC 181 ms 23304 KB
2_33.txt AC 182 ms 27276 KB
2_34.txt AC 175 ms 23052 KB
2_35.txt AC 179 ms 25228 KB
2_36.txt AC 492 ms 33292 KB
2_37.txt AC 471 ms 31884 KB
2_38.txt AC 474 ms 37512 KB
2_39.txt AC 452 ms 33292 KB
2_40.txt AC 477 ms 31500 KB
2_41.txt AC 464 ms 35340 KB
2_42.txt AC 434 ms 33548 KB
2_43.txt AC 487 ms 31372 KB