Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace RoadsNLibraries
- {
- class RoadsNLibraries
- {
- static Dictionary<int, List<int>> adj = new Dictionary<int, List<int>>();
- static List<bool> visited = new List<bool>();
- static int nodes = 0;
- static long cost = 0;
- static void DFS(int n)
- {
- nodes++;
- visited[n] = true;
- foreach (int i in adj[n])
- {
- if (!visited[i])
- {
- DFS(i);
- }
- }
- return;
- }
- static void clear()
- {
- adj.Clear();
- cost = 0;
- nodes = 0;
- }
- static void Main(String[] args)
- {
- int q = Convert.ToInt32(Console.ReadLine());
- for (int a0 = 0; a0 < q; a0++)
- {
- string[] tokens_n = Console.ReadLine().Split(' ');
- int n = Convert.ToInt32(tokens_n[0]);
- int m = Convert.ToInt32(tokens_n[1]);
- long x = Convert.ToInt64(tokens_n[2]);
- long y = Convert.ToInt64(tokens_n[3]);
- for (int a1 = 0; a1 < m; a1++)
- {
- string[] tokens_city_1 = Console.ReadLine().Split(' ');
- int city_1 = Convert.ToInt32(tokens_city_1[0]);
- int city_2 = Convert.ToInt32(tokens_city_1[1]);
- if (adj.ContainsKey(city_1))
- {
- adj[city_1].Add(city_2);
- }
- else
- {
- adj.Add(city_1, new List<int>() { city_2 });
- }
- if (adj.ContainsKey(city_2))
- {
- adj[city_2].Add(city_1);
- }
- else
- {
- adj.Add(city_2, new List<int>() { city_1 });
- }
- }
- cost = 0;
- visited = Enumerable.Repeat(false, n + 1).ToList();
- for (int i = 1; i <= n; ++i)
- {
- if (!visited[i])
- {
- if(adj.ContainsKey(i))
- DFS(i);
- cost = cost + x;
- if (x > y)
- cost = cost + (y * Math.Max(0,(nodes - 1)));
- else
- cost = cost + (x * Math.Max(0, (nodes - 1)));
- }
- }
- Console.WriteLine(cost);
- clear();
- }
- Console.Read();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement