Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int a[1001][1001];
- int n,m,chuaxet[1001],u,v,s;
- //n dinh, m canh, u v la i->j, s bat dau tu dinh s
- void khoitao(){
- for(int i=1;i<=n;i++) chuaxet[i]=true;
- }
- void DFS(int u){
- chuaxet[u]=false;
- for(int v=1;v<=n;v++)
- if(a[u][v]==1 && chuaxet[v]==true){
- DFS(v);
- }
- }
- int ktcanhcau(int u, int v){
- khoitao();
- DFS(1);
- for(int i=1;i<=n;i++){
- if(chuaxet[i]==true) return 1;
- }
- return 0;
- }
- void timcanhcau(){
- for(int i=1;i<=n-1;i++)
- for(int j=i+1;j<=n;j++)
- if(a[i][j]==1){
- a[i][j]=a[j][i]=0;
- if(ktcanhcau(i,j)==1){
- cout<<i<<" "<<j<<" ";
- }
- a[i][j]=a[j][i]=1;
- }
- }
- int main(){
- ios_base::sync_with_stdio(0);cin.tie(0);
- int t;
- cin>>t;
- while(t--){
- cin>>n>>m;
- for(int i=1;i<=n;i++)
- for(int j=1;j<=n;j++)
- a[i][j]=0;
- for(int i=1;i<=m;i++){
- cin>>u>>v;
- a[u][v]=1;
- a[v][u]=1;
- }
- timcanhcau();
- cout<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement