답안 #246487

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
246487 2020-07-09T12:10:00 Z moonrabbit2 Logistical Metropolis (KRIII5_LM) C++17
100 / 7
993 ms 64112 KB
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdb;
typedef tuple<int,int,int> tii;
typedef tuple<ll,ll,ll> tll;
typedef tuple<int,int,int,int> ti4;
typedef vector<vector<ll>> mat;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<> gen(1,100); //gen(rng)
ll modinv(ll a,ll m){
	if(m==1) return 0;
	a%=m;
	if(a<0) a+=m;
	if(a==1) return 1;
	return m-modinv(m,a)*m/a;
}
template <int MOD_> struct modnum{
private:
	int v;
public:
	static const int MOD = MOD_;
 
	modnum() : v(0) {}
	modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
	explicit operator int () const { return v; }
	friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; }
	friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; }
	friend bool operator < (const modnum& a, const modnum& b) { return a.v < b.v; }
	friend bool operator <= (const modnum& a, const modnum& b) { return a.v <= b.v; }
	friend bool operator > (const modnum& a, const modnum& b) { return a.v > b.v; }
	friend bool operator >= (const modnum& a, const modnum& b) { return a.v >= b.v; }
 
	modnum operator ~ () const {
		modnum res;
		res.v = modinv(v, MOD);
		return res;
	}
 
	modnum& operator += (const modnum& o) {
		v += o.v;
		if (v >= MOD) v -= MOD;
		return *this;
	}
	modnum& operator -= (const modnum& o) {
		v -= o.v;
		if (v < 0) v += MOD;
		return *this;
	}
	modnum& operator *= (const modnum& o) {
		v = int(ll(v) * ll(o.v) % MOD);
		return *this;
	}
	modnum& operator /= (const modnum& o) {
		return *this *= (~o);
	}

	
	modnum operator-() const { return modnum(-v); }
	modnum& operator++() { return *this += 1; }
	modnum operator++(int){ modnum tmp=*this; ++*this; return tmp; }
	modnum& operator--() { return *this -= 1; }
	modnum operator--(int){ modnum tmp=*this; --*this; return tmp; }
 
	friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; }
	friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; }
	friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; }
	friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; }
	friend modnum pow(modnum a, ll p) {
		modnum ans = 1;
		for (; p; p /= 2, a *= a) if (p&1) ans *= a;
		return ans;
	}
	friend ostream& operator<<(std::ostream& os, const modnum& o)
	{
		os << o.v;
		return os;
	}
	friend istream& operator>>(std::istream& is, modnum& o)
	{
		is >> o.v;
		return is;
	}
};
using mi=modnum<998244353>;
const ll mod=1e9+7,inf=1e18;
const int N=5e5+5,M=35,K=1e7+5;
int n,m,k,deg[N],in[N],out[N],cnt,h[N],p[17][N],mx[17][N];
int arr[N];
tii e[N],e2[N];
ll ans,cur;
vector<pii> g[N],E[N];
struct UF{
	int p[N]={0};
	int Fi(int x){
		if(!p[x]) return x;
		return p[x]=Fi(p[x]);
	}
	bool Un(int x,int y){
		x=Fi(x); y=Fi(y);
		if(x==y) return false;
		p[y]=x; return true;
	}
}U;
void dfs(int u){
	in[u]=++cnt;
	for(auto [v,c] : g[u]) if(v!=p[0][u]){
		h[v]=h[u]+1; p[0][v]=u; mx[0][v]=c; dfs(v);
	}
	out[u]=cnt;
}
int lca(int u,int v){
	if(h[u]>h[v]) swap(u,v);
	int d=h[v]-h[u];
	for(int bit=16;bit>=0;bit--) if(d&(1<<bit)) v=p[bit][v];
	if(u==v) return u;
	for(int bit=16;bit>=0;bit--) if(p[bit][u]!=p[bit][v]){
		u=p[bit][u]; v=p[bit][v];
	}
	return p[0][u];
}
int main(){
	ios::sync_with_stdio(false); cin.tie(0);
	cin>>n>>m;
	for(int u,v,c,i=1;i<=m;i++){
		cin>>u>>v>>c;
		E[u].emplace_back(v,c);
		E[v].emplace_back(u,c);
		deg[u]++; deg[v]++;
		e[i]=tii(c,u,v);
	}
	sort(e+1,e+1+m);
	for(int i=1;i<=m;i++){
		auto [c,u,v]=e[i];
		if(!U.Un(u,v)) continue;
		ans+=c;
		g[u].emplace_back(v,c);
		g[v].emplace_back(u,c);
	}
	dfs(1);
	for(int bit=1;bit<17;bit++) for(int i=1;i<=n;i++){
		p[bit][i]=p[bit-1][p[bit-1][i]];
		mx[bit][i]=max(mx[bit-1][i],mx[bit-1][p[bit-1][i]]);
	}
	for(int i=1;i<=n;i++){
		k=0;
		arr[++k]=i;
		cur=ans;
		for(auto [v,c] : E[i]){
			arr[++k]=v;
		}
		sort(arr+1,arr+1+k,[](int a,int b){
			return in[a]<in[b];
		});
		for(int j=1;j<=deg[i];j++){
			arr[++k]=lca(arr[j],arr[j+1]);
		}
		sort(arr+1,arr+1+k,[](int a,int b){
			return in[a]<in[b];
		});
		k=unique(arr+1,arr+1+k)-arr-1;
		for(int j=1;j<=k;j++) U.p[arr[j]]=0;
		for(auto [v,c] : E[i]){
			U.Un(i,v);
			cur+=c;
		}
		stack<int> stk; stk.emplace(arr[1]);
		for(int j=2;j<=k;j++){
			int u=arr[j];
			while(in[u]<in[stk.top()]||out[stk.top()]<in[u]) stk.pop();
			int val=0,d=h[u]-h[stk.top()],v=u;
			for(int bit=16;bit>=0;bit--) if(d&(1<<bit)){
				val=max(val,mx[bit][v]); v=p[bit][v];
			}
			cur-=val;
			e2[j-1]=tii(val,u,stk.top());
			stk.emplace(u);
		}
		sort(e2+1,e2+k);
		for(int j=1;j<k;j++){
			auto [c,u,v]=e2[j];
			if(!U.Un(u,v)) continue;
			cur+=c;
		}
		cout<<cur<<"\n";
	}
	return 0;
}

Compilation message

LM.cpp: In function 'int main()':
LM.cpp:154:16: warning: unused variable 'c' [-Wunused-variable]
   for(auto [v,c] : E[i]){
                ^
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 24448 KB Output is correct
2 Correct 23 ms 24576 KB Output is correct
3 Correct 28 ms 24576 KB Output is correct
4 Correct 23 ms 24572 KB Output is correct
5 Correct 24 ms 24576 KB Output is correct
6 Correct 28 ms 24576 KB Output is correct
7 Correct 23 ms 24576 KB Output is correct
8 Correct 22 ms 24576 KB Output is correct
9 Correct 22 ms 24448 KB Output is correct
10 Correct 22 ms 24576 KB Output is correct
11 Correct 22 ms 24576 KB Output is correct
12 Correct 22 ms 24576 KB Output is correct
13 Correct 22 ms 24576 KB Output is correct
14 Correct 22 ms 24576 KB Output is correct
15 Correct 23 ms 24576 KB Output is correct
16 Correct 22 ms 24576 KB Output is correct
17 Correct 23 ms 24576 KB Output is correct
18 Correct 22 ms 24576 KB Output is correct
19 Correct 23 ms 24576 KB Output is correct
20 Correct 22 ms 24576 KB Output is correct
21 Correct 22 ms 24576 KB Output is correct
22 Correct 22 ms 24576 KB Output is correct
23 Correct 22 ms 24576 KB Output is correct
24 Correct 23 ms 24576 KB Output is correct
25 Correct 19 ms 24064 KB Output is correct
26 Correct 19 ms 24192 KB Output is correct
27 Correct 19 ms 24192 KB Output is correct
28 Correct 20 ms 24320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 842 ms 56224 KB Output is correct
2 Correct 847 ms 62912 KB Output is correct
3 Correct 870 ms 62840 KB Output is correct
4 Correct 934 ms 62816 KB Output is correct
5 Correct 934 ms 62840 KB Output is correct
6 Correct 705 ms 63160 KB Output is correct
7 Correct 733 ms 62968 KB Output is correct
8 Correct 736 ms 63220 KB Output is correct
9 Correct 663 ms 64108 KB Output is correct
10 Correct 683 ms 63348 KB Output is correct
11 Correct 637 ms 63768 KB Output is correct
12 Correct 756 ms 64112 KB Output is correct
13 Correct 713 ms 62968 KB Output is correct
14 Correct 759 ms 63348 KB Output is correct
15 Correct 680 ms 63472 KB Output is correct
16 Correct 877 ms 61688 KB Output is correct
17 Correct 818 ms 62968 KB Output is correct
18 Correct 858 ms 62520 KB Output is correct
19 Correct 822 ms 61704 KB Output is correct
20 Correct 828 ms 63096 KB Output is correct
21 Correct 862 ms 61688 KB Output is correct
22 Correct 993 ms 61856 KB Output is correct
23 Correct 917 ms 62712 KB Output is correct
24 Correct 787 ms 61440 KB Output is correct
25 Correct 23 ms 24448 KB Output is correct
26 Correct 23 ms 24576 KB Output is correct
27 Correct 28 ms 24576 KB Output is correct
28 Correct 23 ms 24572 KB Output is correct
29 Correct 24 ms 24576 KB Output is correct
30 Correct 28 ms 24576 KB Output is correct
31 Correct 23 ms 24576 KB Output is correct
32 Correct 22 ms 24576 KB Output is correct
33 Correct 22 ms 24448 KB Output is correct
34 Correct 22 ms 24576 KB Output is correct
35 Correct 22 ms 24576 KB Output is correct
36 Correct 22 ms 24576 KB Output is correct
37 Correct 22 ms 24576 KB Output is correct
38 Correct 22 ms 24576 KB Output is correct
39 Correct 23 ms 24576 KB Output is correct
40 Correct 22 ms 24576 KB Output is correct
41 Correct 23 ms 24576 KB Output is correct
42 Correct 22 ms 24576 KB Output is correct
43 Correct 23 ms 24576 KB Output is correct
44 Correct 22 ms 24576 KB Output is correct
45 Correct 22 ms 24576 KB Output is correct
46 Correct 22 ms 24576 KB Output is correct
47 Correct 22 ms 24576 KB Output is correct
48 Correct 23 ms 24576 KB Output is correct
49 Correct 19 ms 24064 KB Output is correct
50 Correct 19 ms 24192 KB Output is correct
51 Correct 19 ms 24192 KB Output is correct
52 Correct 20 ms 24320 KB Output is correct