PAT甲級1021題,部分測試點無法通過
#include <cstdio>
#include <vector>
#include <algorithm>
namespace PATA1021{
using namespace std;
const int MAXN=10001;
int n,maxH=0;
int father[MAXN];
bool isRoot[MAXN];
vector<int> adt[MAXN],ans,temp;
int findFather(int v){
int ori=v;
while(ori!=father[ori]){
ori=father[ori];
}
while(v!=father[v]){
int tempV=v;
v=father[v];
father[tempV]=ori;
}
return ori;
}
void unionSet(int s1,int s2){
int fa1=findFather(s1);
int fa2=findFather(s2);
if(fa1!=fa2){
father[fa2]=fa1;
}
}
void init(){
for(int i=1;i<=n;i++){
father[i]=i;
isRoot[i]=false;
}
}
int calBlocks(){
int blocks=0;
for(int i=1;i<=n;i++){
isRoot[findFather(i)]=true;
}
for(int i=1;i<=n;i++){
if(isRoot[i]){
blocks++;
}
}
return blocks;
}
void dfs(int now,int pre,int h){
if(h>=maxH){
if(h>maxH){
maxH=h;
temp.clear();
}
temp.push_back(now);
}
for(int i=0;i<adt[now].size();i++){
if(adt[now][i]!=pre){
dfs(adt[now][i],now,++h);
}
}
}
}
紅色字標記處是測試點無法通過的地方,只要把 ++h 換成 h+1 即可通過全部測試點,這是因為h是要傳遞下去的不能隨便更改值,除錯了好一會兒,才明白過來。
如果本篇文章對你有幫助的話,請不要吝惜你的贊哦( ̄▽ ̄)~*