题意:有n个点和m条边(m>2*n),保证图是强联通的,问去掉哪些边,图依然是强联通的(保证一定有可行解)
解题思路:因为图是强联通的,所以从1必然可以走到其他点,其他点也必然可以走到1。1可以走到其他点,那么建正图后从1开始跑,跑过的边就是需要的边;其他点可以走到1,那么建反图后也从1开始跑,跑过的边也是需要的边
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
#include <ctime>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int n, m, u[100009], v[100009];
int vis[100009], ans[100009];
vector<pair<int, int>>g1[100009], g2[100009];
void dfs(int k, vector<pair<int, int>>*x)
{
vis[k] = 1;
for (int i = 0; i < x[k].size(); i++)
{
if (vis[x[k][i].first]) continue;
ans[x[k][i].second] = 1;
dfs(x[k][i].first, x);
}
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
{
g1[i].clear(), g2[i].clear();
vis[i] = 0;
}
for (int i = 1; i <= m; i++)
{
ans[i] = 0;
scanf("%d%d", &u[i], &v[i]);
g1[u[i]].push_back(make_pair(v[i], i));
g2[v[i]].push_back(make_pair(u[i], i));
}
dfs(1, g1);
for (int i = 1; i <= n; i++) vis[i] = 0;
dfs(1, g2);
int tmp = m - 2 * n, pos = 1;
while (tmp)
{
if (!ans[pos])
{
tmp--;
printf("%d %d\n", u[pos], v[pos]);
}
pos++;
}
}
return 0;
}