-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroadlib.cpp
More file actions
executable file
·78 lines (72 loc) · 1.08 KB
/
roadlib.cpp
File metadata and controls
executable file
·78 lines (72 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define int long long int
#define pb push_back
#define mk make_pair
#define flp(i, k, n) for(int i=k; i<n; i++)
#define F first
#define S second
#define vi vector<int>
#define SIZE 200000
bool vis[SIZE];
vi adj[SIZE];
int dfs(int root)
{
int nodes=1LL;
stack<int> s;
s.push(root);
vis[root]=true;
while(!s.empty())
{
int u=s.top();
s.pop();
flp(i, 0, adj[u].size())
{
int v=adj[u][i];
if(!vis[v])
s.push(v);
}
if(!vis[u]){
nodes++;
vis[u]=true;
// cout<<"visited "<<u<<"\n";
}
}
//cout<<nodes<<"\n";
return nodes;
}
int32_t main(void)
{
int t;
cin>>t;
while(t--)
{
int n, m, r, l, ans=0;
cin>>n>>m>>l>>r;
memset(vis, 0, sizeof(vis));
for(int i=0; i<n; i++)
adj[i].clear();
for(int i=0; i<m; i++)
{
int u, v;
cin>>u>>v;
u--; v--;
adj[u].pb(v);
adj[v].pb(u);
}
for(int i=0; i<n; i++)
{
if(!vis[i])
{
int visited=dfs(i);
if(r<l)
ans+=(visited-1)*r+l;
else
ans+=visited*l;
}
}
cout<<ans<<"\n";
}
return 0;
}