当前位置:网站首页>Pat class A - 1013 battle over cities
Pat class A - 1013 battle over cities
2022-06-23 01:21:00 【S atur】
The question : Here's a map for you ,n Cities 、m Strip road , Now I ask you after destroying all the roads connected to a city , At least how many roads need to be built to connect all the remaining cities . One k Group test query .
Ideas : It is equivalent to deleting a point in the connection block and the number of remaining connection blocks -1 The problem of . Ideas 1: Union checking set , Train of thought two :bfs Traverse . Note the upper limit 1e3 A little bit , So at most 1e6 One side , The capacity is not large enough. The last test point cannot pass .
And look up the set code :
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e6+10;
int n, m, k, ans;
int f[N];
int find(int x){
return f[x]==x ? x : f[x]=find(f[x]);
}
void join(int x, int y){
int a = find(x), b = find(y);
if(a != b){
f[a] = b;
ans --; // When building the edge, we should judge , The number of ancestor nodes cannot be determined in the final traversal
}
}
struct node{
int a, b;
} g[N];
signed main()
{
cin >> n >> m >> k;
for(int i = 1; i <= m; i ++){
cin >> g[i].a >> g[i].b;
}
while(k--){
int x; cin >> x;
for(int j = 1; j <= n; j ++) f[j] = j;
ans = n-1;
for(int j = 1; j <= m; j ++){
if(g[j].a==x||g[j].b==x) continue;
join(g[j].a, g[j].b);
// cout << "a: " << g[j].a << " b: " << g[j].b << endl;
}
cout << ans-1 << endl;
}
return 0;
}
dfs Code :
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1005;
int n, m, k, ans;
bool vis[N];
int g[N][N];
void dfs(int x){ // dfs Run all the points you can reach
vis[x] = 1;
for(int i = 1; i <= n; i ++){
if(!vis[i]&&g[x][i] == 1){
dfs(i);
}
}
}
signed main()
{
cin >> n >> m >> k;
for(int i = 1; i <= m; i ++){
int a, b; cin >> a >> b;
g[a][b] = g[b][a] = 1;
}
while(k--){
int x; cin >> x;
memset(vis, 0, sizeof(vis));
vis[x] = 1; ans = 0;
for(int i = 1; i <= n; i ++){ // For each node that has not been visited dfs
if(!vis[i]){
dfs(i);
ans ++;
}
}
cout << ans-1 << endl;
}
return 0;
}
边栏推荐
- 一文读懂基于Redis的Amazon MemoryDB数据库
- 手机上券商开户哪个券商平台更好更安全,如果需要佣金低的怎么办
- The longest child sequence of the 2019 Blue Bridge Cup
- I've been outsourcing for four years, but I feel it's useless
- Steps to implement a container global component
- How about China International Futures Co., Ltd.? Is it a regular futures company? Is it safe to open an account online?
- How to refine permissions to buttons?
- You can also do NLP (classification)
- Overview of visual object detection technology based on deep learning
- Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;
猜你喜欢

How to get started with machine learning?

How to refine permissions to buttons?

3D打印微组织

MySQL - SQL execution process

Yyds dry inventory solution sword finger offer: print the binary tree into multiple lines

Cadence spb17.4 - Allegro - optimize and specify the polyline connection angle of a single electrical line - polyline to arc

How about precious metal spot silver?

A hundred lines of code to realize reliable delay queue based on redis

Cadence spb17.4 - Allegro - optimiser la spécification de l'angle de connexion de la polyligne pour une seule ligne électrique - polyligne à arc

Binary tree to string and string to binary tree
随机推荐
C#. Net universal database access encapsulation classes (access, sqlserver, Oracle)
Cadence spb17.4 - Chinese UI settings
Population standard deviation and sample standard deviation
07 project cost management
層次選擇器
Yyds dry inventory solution sword finger offer: print the binary tree into multiple lines
C#.NET万能数据库访问封装类(ACCESS、SQLServer、Oracle)
Vector 1 (classes and objects)
3DMAX modeling notes (I): introducing 3DMAX and creating the first model Hello World
Dig three feet to solve the data consistency problem between redis and MySQL
New progress in the construction of meituan's Flink based real-time data warehouse platform
Hello, is the securities account presented by the Business School of qiniu business school safe? How can I open a safe stock account to speculate in stocks
SAP ui5 application development tutorial 103 - how to consume the trial version of the third-party library in SAP ui5 applications
[sliding window] leetcode992 Subarrays with K Different Integers
Js--- SVG to png
Vector 6 (inheritance)
Add / get and delete cookies
What aspects of language and database knowledge are needed to build a web Kanban!
Cadence spb17.4 - Allegro - optimize and specify the polyline connection angle of a single electrical line - polyline to arc
BGP联邦综合实验