101342024-03-27 21:10:07111Vázsony vonatjegyet vásárolcpp17Accepted 100/100860ms179800 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

#define INF (int)1e18

struct DSU {
	vector<int> v;

	DSU(int n) : v(n + 1, -1) {
	}

	int find(int a) {
		return v[a] < 0 ? a : v[a] = find(v[a]);
	}

	void unite(int a, int b) {
		a = find(a);
		b = find(b);
		if (a == b) {
			return;
		}
		if (v[a] > v[b]) {
			swap(a, b);
		}
		v[a] += v[b];
		v[b] = a;
	}
};

template<typename T>
using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N,M,A,B;
	cin>>N>>M>>A>>B;
	vector<vector<pair<int,int>>>g(N+1);
	for(int i=0;i<M;i++){
		int a,b,w;
		cin>>a>>b>>w;
		g[a].emplace_back(b,w);
		g[b].emplace_back(a,w);
	}
	vector<int>c(N+1,INF);
	min_priority_queue<pair<int,int>>pq;
	c[B]=0;
	pq.emplace(0,B);
	while(!pq.empty()){
		auto[d,i]=pq.top();
		pq.pop();
		if(d>c[i]){
			continue;
		}
		for(auto[j,w]:g[i]){
			if(c[i]+w<c[j]){
				c[j]=c[i]+w;
				pq.emplace(c[j],j);
			}
		}
	}
	vector<int>v(N+1);
	DSU dsu(N+1);
	auto dfs1=[&](auto self,int i)->void{
		if(v[i]){
			return;
		}
		v[i]=1;
		for(auto[j,_]:g[i]){
			if(c[j]==c[i]){
				dsu.unite(i,j);
			}
			if(c[j]<=c[i]){
				self(self,j);
			}
		}
	};
	dfs1(dfs1,A);
	vector<set<int>>gg(N+1);
	for(int i=1;i<=N;i++){
		for(auto[j,_]:g[i]){
			if(c[j]<=c[i]&&dsu.find(j)!=dsu.find(i)){
				gg[dsu.find(i)].insert(dsu.find(j));
			}
		}
	}
	vector<int>w(N+1),u(N+1);
	auto dfs2=[&](auto self,int i)->void{
		if(w[i]){
			return;
		}
		int x=0;
		for(int j:gg[i]){
			self(self,j);
			if(w[j]>x){
				x=w[j];
				u[i]=j;
			}
		}
		w[i]=-dsu.v[dsu.find(i)]+x;
	};
	dfs2(dfs2,dsu.find(A));
	vector<int>y(N+1);
	for(int i=dsu.find(A);i;i=u[i]){
		y[i]=1;
	}
	cout<<w[dsu.find(A)]<<'\n';
	for(int i=1;i<=N;i++){
		if(y[dsu.find(i)]){
			cout<<i<<' ';
		}
	}
	cout<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1888 KiB
2Accepted140ms38324 KiB
subtask220/20
3Accepted59ms20260 KiB
4Accepted435ms104180 KiB
5Accepted298ms81328 KiB
6Accepted238ms69564 KiB
7Accepted324ms81628 KiB
8Accepted233ms65344 KiB
9Accepted546ms135100 KiB
subtask315/15
10Accepted160ms36220 KiB
11Accepted13ms6640 KiB
12Accepted12ms5688 KiB
13Accepted35ms11456 KiB
14Accepted93ms32560 KiB
subtask420/20
15Accepted71ms34776 KiB
16Accepted243ms60076 KiB
17Accepted41ms27876 KiB
18Accepted83ms42208 KiB
19Accepted37ms21468 KiB
20Accepted82ms37584 KiB
subtask545/45
21Accepted3ms4684 KiB
22Accepted860ms179800 KiB
23Accepted52ms20236 KiB
24Accepted230ms64708 KiB
25Accepted199ms58424 KiB
26Accepted368ms88708 KiB
27Accepted187ms57852 KiB
28Accepted642ms143300 KiB
29Accepted137ms40356 KiB
30Accepted646ms119888 KiB
31Accepted224ms74332 KiB
32Accepted48ms15452 KiB
33Accepted323ms84840 KiB
34Accepted264ms77516 KiB
35Accepted294ms72804 KiB
36Accepted259ms63140 KiB
37Accepted256ms59008 KiB