Gym-101630C Connections
作者:互联网
Connections
思维
真没看出来
由于给的是强连通图,直接保留从 \(1\) 开始深搜,保证 \(1\) 能访问到其他所有点的,然后反向建图,保证所有点能够到达 \(1\)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 1e5 + 10;
vector<int>gra[2][maxn];
int ans[2][maxn];
int vis[maxn];
void dfs(int now, int pre, int f)
{
if(ans[f][now]) return;
ans[f][now] = pre;
for(int nex : gra[f][now])
dfs(nex, now, f);
}
int main()
{
int t;
cin >> t;
while(t--)
{
int n, m;
cin >> n >> m;
for(int i=0; i<2; i++)
{
for(int j=0; j<=n; j++)
{
gra[i][j].clear();
ans[i][j] = 0;
}
}
for(int i=0; i<m; i++)
{
int a, b;
cin >> a >> b;
gra[0][a].push_back(b);
gra[1][b].push_back(a);
}
dfs(1, 1, 1);
dfs(1, 1, 0);
int cnt = m - 2 * n;
int cur = 0;
for(int i=1; i<=n && cnt; i++)
{
for(int j=0; j<gra[0][i].size() && cnt; j++)
{
int nex = gra[0][i][j];
if(ans[0][nex] == i || ans[1][i] == nex) continue;
if(cur++) cout << " ";
cout << i << " " << nex << "\n";
cnt--;
}
}
}
return 0;
}
标签:now,101630C,int,Gym,dfs,Connections,maxn,ans,include 来源: https://www.cnblogs.com/dgsvygd/p/16611137.html