【DFS】HDU 5423 Rikka with Tree
/*
DFS
Rikka with Tree
時間: 2017/02/24
題意:判斷樹是否存在跟他相似並不同的樹
題解:
要求相似並不同,樹的結構必須除了最後一層,每層只能一個點
最開始我用記憶化搜尋去計算其長度,因為我覺得樹都是已知指向未知,相當於DAG,但是一直wa。(困惑)
後來用dfs計算長度過了
*/
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
#define LL long long
#define N 1010
#define INF 0x3f3f3f3f
int mp[N][N],dist[N],vis[N];
int n;
int DFS(int i)
{
if(dist[i] >= 0)
return dist[i];
for(int j = 1; j < n; j++)
{
if(mp[i][j] && dist[j] >= 0)
return dist[i] = 1+DFS(j);
}
}
void dfs(int u,int depth)
{
dist[u]=depth;
//printf("%d %d\n",u,dist[u]);
for(int i = 1; i <= n; i++)
{
vis[u]=true;
if(mp[i][u] && !vis[i])
{
dfs(i,depth+1);
vis[u]=false;
}
}
}
int main()
{
while(~scanf("%d",&n))
{
memset(mp,0,sizeof(mp));
int a,b;
for(int i = 1; i < n; i++)
{
scanf("%d%d",&a,&b);
mp[a][b] = mp[b][a] = 1;
}
int flag = 1;
memset(dist,-1,sizeof(dist));
memset(vis,0,sizeof(vis));
int mx = -1;
dfs(1,0);
for(int i = 1; i <= n; i++)
{
mx = max(mx,dist[i]);
}
for(int i = 2; i <= n; i++)
{
for(int j = i+1; j <= n; j++)
if(dist[i] == dist[j] && dist[i] != mx)
{
flag = 0;
break;
}
if(!flag)
break;
}
if(flag)
puts("YES");
else
puts("NO");
}
return 0;
}
相關文章
- HDU5425Rikka with Tree II(數學期望)
- hdu 6415 Rikka with Nash EquilibriumUI
- HDU 5831 Rikka with Parenthesis II (括號匹配)
- HDU 6415 Rikka with Nash Equilibrium (DP)UI
- HDU6415:Rikka with Nash Equilibrium(dp)UI
- HDU5424Rikka with Graph II(哈密頓圖判斷)
- hdu1258 Sum It Up (DFS)
- HDU 5113 Black And White (dfs)
- [HDU6793] Tokitsukaze and Colorful Tree
- HDU5293 : Tree chain problemAI
- HDU 4925 Apple Tree (瞎搞)APP
- HDU-6415 Rikka with Nash Equilibrium (DP/找規律)UI
- HDU 5468 Puzzled Elena(DFS序+容斥原理)
- HDU 5438 Ponds (拓撲排序應用+DFS)排序
- Codefroces 1328E Tree Querie(dfs序)
- LeetCode C++ 968. Binary Tree Cameras【Tree/DFS】困難LeetCodeC++
- LeetCode#110.Balanced Binary Tree(Tree/Height/DFS/Recursion)LeetCode
- HDU 5113 Black And White(暴力dfs+減枝)
- POJ 3321 Apple Tree(dfs+樹狀陣列)APP陣列
- CodeForces - 463E Caisa and Tree (dfs+素因子分解)AI
- hdu 5086 Revenge of Segment Tree(BestCoder Round #16)
- HDU6035-Colorful Tree-虛樹思想
- POJ3321 Apple Tree(DFS序 + 樹狀陣列)APP陣列
- HDU5831(2016多校第八場)———Rikka with Parenthesis II(水題)
- hdu 4836 The Query on the Tree(線段樹or樹狀陣列)陣列
- HDU 5200 Tree (離線並查集)並查集
- 【dp+組合數學】hdu 2018 多校第九場 1001 Rikka with Nash Equilibrium hdu 6415UI
- LeetCode C++ 1302. Deepest Leaves Sum【Tree/BFS/DFS】中等LeetCodeC++
- 【記憶優化搜尋/dp】HDU - 6415 - 杭電多校第九場 - Rikka with Nash Equilibrium優化UI
- 2018 Multi-University Training Contest 9----hdu 6415 Rikka with Nash EquilibriumAIUI
- HDU 3333 Turing Tree(線段樹+離線操作)
- HDU 4542 小明系列故事——未知剩餘系 (DFS 反素數 篩子預處理)
- DFS
- HDU 1258Sum It Up(暴力dfs,記住相同的狀態只保留一個)
- [DP]HDU6415(2018多校訓練賽第九場 Problem A) Rikka with Nash Equilibrium 題解UI
- DFS樹
- dfs序
- Binary Tree Traversals(HDU1710)二叉樹的簡單應用二叉樹