2023-2-19
1、并查集
2023-2-24
1、位运算:
if(x&(x-1))//求出x中的一
x>>=1//除2
x&(-x)//得到一个低位1的二进制数
log2(k)//的返回值为double类型
2、图:
题:
C. Bridge
内存限制:256 MiB
时间限制:1000 ms
标准输入输出
题目类型:传统
评测方式:文本比较
题目描述
给你一个个点条边无重边无自环的无向图。
求有几个桥边。删除后使得图不连通的边称为桥边。
You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.
The i-th edge (1 leq i leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a bridge.
Find the number of the edges that are bridges among the M edges.
输入格式
第一行两个正整数,之后行每行两个数描述无向边。
Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M
输出格式
一个正整数代表图里有几个桥。
Print the number of the edges that are bridges among the M edges.
样例
样例输入1
7 7
1 3
2 7
3 4
4 5
4 6
5 6
6 7
样例输入2
3 3
1 2
1 3
2 3
样例输入3
6 5
1 2
2 3
3 4
4 5
5 6
样例输出1
4
样例输出2
0
样例输出3
5
数据范围与提示
2 ≤ N ≤ 50 N − 1 ≤ M ≤ min(N(N − 1) / 2, 50)
代码
#include<bits/stdc++.h>
using namespace std;
int n,m,ans;
int e[55][55];
bool f[55];
pair<int, int> edge[1500];
int x,y;
void dfs(int x){
f[x]=1;
for(int j=1;j<=n;j++){
if(e[x][j]&&!f[j]){
dfs(j);
}
}
}
bool ch(){
memset(f,0,sizeof(f));
dfs(1);
for(int i=1;i<=n;i++){
if(!f[i]){
return 1;
}
}
return 0;
}
int u,v;
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>u>>v;
e[u][v]=e[v][u]=1;
edge[i]=make_pair(u,v);
}
for(int i=1;i<=m;i++){
x=edge[i].first;
y=edge[i].second;
e[x][y]=e[y][x]=0;
ans+=ch();
e[x][y]=e[y][x]=1;
}
cout<<ans;
}
难受😭
考试调皮,被踢出编程舍了