답안 #23155

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
23155 2017-05-03T17:44:50 Z ainta Logistical Metropolis (KRIII5_LM) C++14
7 / 7
1193 ms 48572 KB
#include<cstdio>
#include<algorithm>
#include<vector>
#define pii pair<int,int>
using namespace std;
vector<int>E[101000], T[101000], L[101000];
int n, m, pL[101000][20], par[101000][18], Dep[101000], Num[101000], Ed[101000], cnt;
struct Edge{
    int a, b, c;
    bool operator <(const Edge &p)const{
        return c<p.c;
    }
}w[301000];
int UF[101000];
long long S;
int Find(int a){
    if(a==UF[a])return a;
    return UF[a] = Find(UF[a]);
}
bool Merge(int a, int b){
    a=Find(a),b=Find(b);
    if(a==b)return false;
    UF[a] = b;
    return true;
}
void DFS(int a, int pp){
    int i, x;
    Num[a] = ++cnt;
    for(i=0;i<17;i++){
        par[a][i+1]=par[par[a][i]][i];
        pL[a][i+1]=max(pL[a][i],pL[par[a][i]][i]);
    }
    for(i=0;i<T[a].size();i++){
        x = T[a][i];
        if(x==pp)continue;
        Dep[x] = Dep[a] + 1;
        par[x][0] = a;
        pL[x][0] = L[a][i];
        DFS(x,a);
    }
    Ed[a] = cnt;
}
vector<pii> V;
int LCA(int a, int b){
    if(Dep[a]<Dep[b])return LCA(b,a);
    int d = Dep[a]-Dep[b], i = 0;
    while(d){
        if(d&1)a = par[a][i];
        i++;d>>=1;
    }
    for(i=16;i>=0;i--)if(par[a][i]!=par[b][i])a=par[a][i],b=par[b][i];
    if(a!=b)a=par[a][0];
    return a;
}
int Cur;
vector<Edge>EE;
Edge Get(int a, int p){
    int d = Dep[a] - Dep[p], i = 0, M = 0, aa = a;
    while(d){
        if(d&1){
            M = max(M,pL[a][i]);
            a = par[a][i];
        }
        d>>=1;i++;
    }
    return {aa,p,M};
}
void Make(int a){
    int x = V[a].second;
    Cur++;
    while(1){
        if(Cur==V.size() || V[Cur].first > Ed[x])break;
        EE.push_back(Get(V[Cur].second,x));
        Make(Cur);
    }
}
long long GetMST(){
    int i;
    for(i=0;i<V.size();i++)UF[V[i].second] = V[i].second;
    long long ss = 0;
    for(i=0;i<EE.size();i++){
        if(Merge(EE[i].a,EE[i].b)){
            ss += EE[i].c;
        }
    }
    return ss;
}
void Do(int a, vector<int> &P){
    int i;
    vector<pii> TP;
    for(i=0;i<P.size();i++)TP.push_back({Num[P[i]],P[i]});
    sort(TP.begin(),TP.end());
    V = TP;
    for(i=0;i<TP.size()-1;i++){
        int x = LCA(TP[i].second, TP[i+1].second);
        V.push_back(pii(Num[x],x));
    }
    sort(V.begin(),V.end());
    V.erase(unique(V.begin(),V.end()),V.end());
    Cur = 0;
    Make(0);
    sort(EE.begin(),EE.end());
    long long s1 = GetMST() + E[a].size()* (long long)2e9;
    for(i=0;i<EE.size();i++)if(EE[i].c < 0)EE[i].c += 2e9;
    sort(EE.begin(),EE.end());
    long long s2 = GetMST();
    printf("%lld\n", S + s1 - s2);
}
int main(){
    int i, j;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)UF[i] = i;
    for(i=0;i<m;i++){
        scanf("%d%d%d",&w[i].a,&w[i].b,&w[i].c);
    }
    sort(w,w+m);
    for(i=0;i<m;i++){
        E[w[i].a].push_back(i);
        E[w[i].b].push_back(i);
        if(Merge(w[i].a,w[i].b)){
            T[w[i].a].push_back(w[i].b);
            T[w[i].b].push_back(w[i].a);
            L[w[i].a].push_back(w[i].c);
            L[w[i].b].push_back(w[i].c);
            S += w[i].c;
        }
    }
    DFS(1, 0);
    for(i=1;i<=n;i++){
        vector<int>P;
        EE.clear();
        for(j=0;j<E[i].size();j++){
            Edge tp = w[E[i][j]];
            tp.c -= 2e9;
            EE.push_back(tp);
            P.push_back(tp.a+tp.b-i);
        }
        P.push_back(i);
        Do(i, P);
    }
}

Compilation message

LM.cpp: In function 'void DFS(int, int)':
LM.cpp:33:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(i=0;i<T[a].size();i++){
              ^
LM.cpp: In function 'void Make(int)':
LM.cpp:72:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if(Cur==V.size() || V[Cur].first > Ed[x])break;
               ^
LM.cpp: In function 'long long int GetMST()':
LM.cpp:79:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(i=0;i<V.size();i++)UF[V[i].second] = V[i].second;
              ^
LM.cpp:81:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(i=0;i<EE.size();i++){
              ^
LM.cpp: In function 'void Do(int, std::vector<int>&)':
LM.cpp:91:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(i=0;i<P.size();i++)TP.push_back({Num[P[i]],P[i]});
              ^
LM.cpp:94:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(i=0;i<TP.size()-1;i++){
              ^
LM.cpp:104:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(i=0;i<EE.size();i++)if(EE[i].c < 0)EE[i].c += 2e9;
              ^
LM.cpp: In function 'int main()':
LM.cpp:132:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(j=0;j<E[i].size();j++){
                  ^
LM.cpp:111:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&n,&m);
                        ^
LM.cpp:114:48: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d",&w[i].a,&w[i].b,&w[i].c);
                                                ^
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 29272 KB Output is correct
2 Correct 6 ms 29272 KB Output is correct
3 Correct 6 ms 29272 KB Output is correct
4 Correct 6 ms 29272 KB Output is correct
5 Correct 6 ms 29272 KB Output is correct
6 Correct 9 ms 29272 KB Output is correct
7 Correct 6 ms 29272 KB Output is correct
8 Correct 6 ms 29272 KB Output is correct
9 Correct 0 ms 29420 KB Output is correct
10 Correct 9 ms 29272 KB Output is correct
11 Correct 3 ms 29272 KB Output is correct
12 Correct 6 ms 29272 KB Output is correct
13 Correct 6 ms 29424 KB Output is correct
14 Correct 6 ms 29272 KB Output is correct
15 Correct 6 ms 29272 KB Output is correct
16 Correct 6 ms 29272 KB Output is correct
17 Correct 6 ms 29272 KB Output is correct
18 Correct 6 ms 29272 KB Output is correct
19 Correct 6 ms 29272 KB Output is correct
20 Correct 9 ms 29272 KB Output is correct
21 Correct 9 ms 29272 KB Output is correct
22 Correct 6 ms 29272 KB Output is correct
23 Correct 9 ms 29272 KB Output is correct
24 Correct 0 ms 29272 KB Output is correct
25 Correct 0 ms 29140 KB Output is correct
26 Correct 3 ms 29140 KB Output is correct
27 Correct 3 ms 29140 KB Output is correct
28 Correct 3 ms 29272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 953 ms 41284 KB Output is correct
2 Correct 1008 ms 41284 KB Output is correct
3 Correct 1029 ms 41284 KB Output is correct
4 Correct 996 ms 41284 KB Output is correct
5 Correct 936 ms 41284 KB Output is correct
6 Correct 923 ms 43904 KB Output is correct
7 Correct 926 ms 42288 KB Output is correct
8 Correct 1002 ms 43924 KB Output is correct
9 Correct 903 ms 48572 KB Output is correct
10 Correct 1026 ms 45168 KB Output is correct
11 Correct 906 ms 47160 KB Output is correct
12 Correct 889 ms 46132 KB Output is correct
13 Correct 993 ms 42612 KB Output is correct
14 Correct 826 ms 43576 KB Output is correct
15 Correct 919 ms 45920 KB Output is correct
16 Correct 1139 ms 40912 KB Output is correct
17 Correct 1159 ms 41804 KB Output is correct
18 Correct 1103 ms 41168 KB Output is correct
19 Correct 1059 ms 40744 KB Output is correct
20 Correct 1149 ms 41612 KB Output is correct
21 Correct 1193 ms 40864 KB Output is correct
22 Correct 1189 ms 40968 KB Output is correct
23 Correct 1116 ms 41320 KB Output is correct
24 Correct 1053 ms 40752 KB Output is correct
25 Correct 6 ms 29272 KB Output is correct
26 Correct 6 ms 29272 KB Output is correct
27 Correct 6 ms 29272 KB Output is correct
28 Correct 6 ms 29272 KB Output is correct
29 Correct 6 ms 29272 KB Output is correct
30 Correct 9 ms 29272 KB Output is correct
31 Correct 6 ms 29272 KB Output is correct
32 Correct 6 ms 29272 KB Output is correct
33 Correct 0 ms 29420 KB Output is correct
34 Correct 9 ms 29272 KB Output is correct
35 Correct 3 ms 29272 KB Output is correct
36 Correct 6 ms 29272 KB Output is correct
37 Correct 6 ms 29424 KB Output is correct
38 Correct 6 ms 29272 KB Output is correct
39 Correct 6 ms 29272 KB Output is correct
40 Correct 6 ms 29272 KB Output is correct
41 Correct 6 ms 29272 KB Output is correct
42 Correct 6 ms 29272 KB Output is correct
43 Correct 6 ms 29272 KB Output is correct
44 Correct 9 ms 29272 KB Output is correct
45 Correct 9 ms 29272 KB Output is correct
46 Correct 6 ms 29272 KB Output is correct
47 Correct 9 ms 29272 KB Output is correct
48 Correct 0 ms 29272 KB Output is correct
49 Correct 0 ms 29140 KB Output is correct
50 Correct 3 ms 29140 KB Output is correct
51 Correct 3 ms 29140 KB Output is correct
52 Correct 3 ms 29272 KB Output is correct