D 金蛇狂舞----爆搜
题目链接
两周没写代码,一个爆搜小bug都改半天,麻了。。。。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
const int N = 100010,INF = 0x3f3f3f3f;
int res=INF;
int j[11];
void dfs(int x,int depth,int y)
{
if(depth>7)return ;
if(x==y)
{
res=min(res,depth);
return ;
}
if(x<=10)dfs(j[x],depth+1,y);
int t=sqrt(x);
if(x!=t*t)dfs(t+1,depth+1,y);
dfs(t,depth+1,y);
}
signed main()
{
j[0]=1;
for(int i=1;i<11;i++)j[i]=j[i-1]*i;
int T;
cin>>T;
while(T--)
{
res=INF;
int x,y;
cin>>x>>y;
dfs(x,0,y);
if(res==INF)cout<<-1<<endl;
else cout<<res<<endl;
}
}
F 火凤燎原—容斥原理+蒲公英树
题目链接
八仙过海之蒲公英树概念
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define int long long
const int N = 100010;
int din[N];
signed main()
{
int T;
cin>>T;
while(T--)
{
int n,res=0;
cin>>n;
for(int i=1;i<=n;i++)din[i]=0;
for(int i=1;i<=2*n-2;i++)
{
int x;
scanf("%d",&x);
din[x]++;
}
for(int i=1;i<=n;i++)
{
if(din[i]>=3)res+=n-din[i]-1;
}
cout<<res<<endl;
}
}