// lanqiao3517 砍树(DFS) #include using namespace std; #define int long long const int N = 1e5 + 9; vector> g[N]; bool vis[N]; int n, m, s[N], t[N]; // 暴力解法: 枚举删除的边, 利用dfs查询数据对之间的连通性-时间复杂组为O(n^2m) bool dfs(int s, int t, int id){ if(s == t){ return true; } for(int i = 0; i < g[s].size(); i++){ int nxt = g[s][i].first, edgeId = g[s][i].second; if(edgeId == id) continue; if(vis[nxt]) continue; vis[nxt] = 1; if(dfs(nxt, t, id)) return true; } return false; } signed main(){ cin >> n >> m; for(int i = 1; i < n; i++){ int u, v; cin >> u >> v; g[u].push_back({v, i}); g[v].push_back({u, i}); } for(int i = 1; i <= m; i++){ cin >> s[i] >> t[i]; } // 枚举需要删除的边的编号-从大到小枚举 for(int i = n - 1; i >= 1; i--){ bool flag = true; for(int j = 1; j <= m; j++){ memset(vis, 0, sizeof vis); vis[s[j]] = 1; if(dfs(s[j], t[j], i)){ flag = false; break; } } if(flag){ cout << i << ' '; return 0; } } cout << -1 << endl; return 0; } /* test samples -> 4 6 2 1 2 2 3 4 3 2 5 6 5 3 6 4 5 */