Submission #23155

#TimeUsernameProblemLanguageResultExecution timeMemory
23155aintaLogistical Metropolis (KRIII5_LM)C++14
7 / 7
1193 ms48572 KiB
#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 (stderr)

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);
                                                ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...