暴力代码,不能ac ,只能过百分之三十的数据
细节:
#include <iostream>
#include<map>
#include<vector>
#define int long long
#define int long long
using namespace std;
const int N = 1e5 + 10;
typedef pair<int, int> pii;
vector<int>edge[N];
int n, m;
int w[N];//从每一个边的边权。
map<pii, int>id;//存边的编号
//start是路径的起点,dest是路径的重终点,current表示你当前走到了哪个点。
bool dfs(int start, int current, int father, int dest)
{
if (current == dest)
{
return true;
}
for (int i = 0; i < edge[current].size(); i++)
{
int child = edge[current][i];
//防止死循环
if (child == father)
continue;
if (dfs(start, child, current, dest) ==true )
{
int ID = id[{current, child}];
w[ID] ++;
return true;
}
}
return false;
}
void solve()
{
cin >> n >> m;
for (int i = 0; i < n - 1; i++)
{
int x, y; cin >> x >> y;
edge[x].push_back(y);
edge[y].push_back(x);
id[{x, y}] = id[{y, x}] = i;
}
for (int i = 0; i < m; i++)
{
int x, y; cin >> x >> y;
dfs(x, x, -1, y);
}
int ans = -1;
for (int i = n - 1; i >= 0; i--)
{
if (w[i] == m)
{
ans = i + 1;
break;
}
}
cout << ans << endl;
}
signed main()
{
int t = 1;
// cin >> t;
while (t--)
solve();
}