白夜 题解
白夜(Apocalypse)
解题思路
因为只用断开
若方案数为 No,无解,否则输出 Yes。时间复杂度
正解
根据上面的解题思路,可以发现每次断开一条边后只有这条边的两个端点会受到影响,而其他点的度一定大于
因此每次枚举每一个点,然后枚举这个点所连接的边(使用邻接表),若这条边的两个端点的度均大于
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5+75;
int T , n , ans , t[N];
vector <int> e[N];
int main()
{
ios::sync_with_stdio (false);
cin >> T;
while (T --)
{
cin >> n;
ans = 0;
for(int i = 1 ; i <= n ; i ++)
{
e[i].clear ();
t[i] = 0;
}
for(int i = 1 ; i <= n-1 ; i ++)
{
int u , v;
cin >> u >> v;
e[u].push_back (v);
e[v].push_back (u);
++ t[u] , ++ t[v];
}
for(int i = 1 ; i <= n ; i ++)
{
if(t[i] > 1)
{
for(auto j : e[i])
{
if(t[j] > 1)
++ ans;
}
}
}
if(!ans) cout << "No\n";
else cout << "Yes " << ans/2 << "\n";
}
return 0;
}