-2
这样的DFS功能可按代码片段: -我不理解的语法这DFS实现
void dfs(int u, int p)
{
if(p!=-1)d[u] = d[p]+1;
for(int i: v[u])
{
if(i==p)continue;
dfs(i, u);
}
}
我不理解这其中的DFS排在比赛的社论执行。完整的代码是follows.it将是非常好的,如果有人可以帮助我理解这一段代码
#include <bits/stdc++.h>
using namespace std;
#define int long long int
vector<int> d;
vector< vector<int> > v;
void dfs(int u, int p)
{
if(p!=-1)d[u] = d[p]+1;
for(int i: v[u])
{
if(i==p)continue;
dfs(i, u);
}
}
#undef int
int main()
{
#define int long long int
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
v.resize(n);
d.resize(n);
for(int i = 0;i<n-1;i++)
{
int x, y;
cin>>x>>y;
v[x-1].push_back(y-1);
v[y-1].push_back(x-1);
}
d[0] = 0;
dfs(0, -1);
int q;
cin>>q;
while(q--)
{
int x, y;
cin>>x>>y;
if((d[x-1]+d[y-1])&1)cout<<"Odd"<<endl;
else cout<<"Even"<<endl;
}
return 0;
}
代码中写的不好,没有人真正应该使用快速黑客替换'](http://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h);命名错误的变量;没有文件或评论。如果你想学习编程C++,那么[阅读好书](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)或去学校。 –
for-loop
代码学习材料。案例:用宏重新定义一种基本的内置类型; [包括'没有什么像重新定义关键字那样激发你的代码。接下来'#define double float' –
@Someprogrammerdude几天前,我正在审查一些考试试卷,以便在我们的办公室进行新一期招聘。我感到惊讶的是,每个人都包含'bits/stdC++。h'。我问了其他人,现在主要关注的是哪些书,我的一位年轻同事回答说'Code :: Blocks'自动生成这个。 – taskinoor