本文整理汇总了C++中dfs函数的典型用法代码示例。如果您正苦于以下问题:C++ dfs函数的具体用法?C++ dfs怎么用?C++ dfs使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了dfs函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: maxPathSum
int maxPathSum(TreeNode *root) {
max_sum = INT_MIN;
dfs(root);
return max_sum;
}
示例2: NestedIterator
NestedIterator(vector<NestedInteger> &nestedList) {
index = 0;
dfs(nestedList);
}
示例3: main
int main(void)
{
FILE * fp;
char line[256];
char * name;
int numConnections;
int familyNum;
int i;
int parent1, parent2, child;
int start, target;
// Open the input file
fp = fopen("family.in", "r");
// Start the family count at 1
familyNum = 1;
// Read in the number of connections
fgets(line, sizeof(line), fp);
numConnections = atoi(line);
while (numConnections > 0)
{
// Initialize our data structures that hold the family tree information
numNames = 0;
memset(names, 0, sizeof(names));
memset(connections, 0, sizeof(connections));
// Read in each connection
for (i = 0; i < numConnections; i++)
{
// Read the entire line
fgets(line, sizeof(line), fp);
// Parse the first name on the line and get it from (or add it to)
// the list
name = strtok(line, " \n\r");
parent1 = getOrAddName(name);
// Parse the second name on the line and get it from (or add it to)
// the list
name = strtok(NULL, " \n\r");
parent2 = getOrAddName(name);
// Parse the third name on the line and... you get the idea
name = strtok(NULL, " \n\r");
child = getOrAddName(name);
// Enter the connections in our connection matrix
connections[parent1][child] = true;
connections[child][parent1] = true;
connections[parent2][child] = true;
connections[child][parent2] = true;
}
// Read the next line and get the two people we need to check
fgets(line, sizeof(line), fp);
name = strtok(line, " \n\r");
start = getOrAddName(name);
name = strtok(NULL, " \n\r");
target = getOrAddName(name);
// Now, run the DFS to see if the two people are related
memset(visited, 0, sizeof(visited));
if (dfs(start, target))
printf("Family #%d: Related.\n", familyNum);
else
printf("Family #%d: Not related.\n", familyNum);
// Read in the number of connections in the next family tree
familyNum++;
fgets(line, sizeof(line), fp);
numConnections = atoi(line);
}
// Close the input file
fclose(fp);
return 0;
}
示例4: main
int main(){
int G[6][6]={{0,1,0,0,0,1},{0,0,1,0,1,0},{0,0,0,1,0,0},{0,1,0,0,0,0},{0,0,1,0,0,1},{0,0,0,0,0,0}};
char E[6][6];
dfs(G,E);
}
示例5: hasPathSum
bool hasPathSum(TreeNode *root, int sum) {
if(!root) return false;
return dfs(root, sum, 0);
}
示例6: bstFromPreorder
TreeNode* bstFromPreorder(vector<int>& preorder) {
int i = 0;
return dfs(preorder, i, INT_MAX);
}
开发者ID:jiadaizhao,项目名称:LeetCode,代码行数:4,代码来源:1008-Construct+Binary+Search+Tree+from+Preorder+Traversal.cpp
示例7: predo
int predo(){
dfs(1);
for (int i = 1;i <= 20;i++)
for (int j = 1; j <= n;j++)
dp[j][i] = dp[dp[j][i-1]][i-1];
}
示例8: main
int main(void)
{
int i,f,m,n,a,b,num,x,y,max;
ji=0;
while(scanf("%d%d",&m,&n),!(0==m&&0==n))
{
ji++;
for(i=0;i<=m+1;i++)
{
for(f=0;f<=n+1;f++)
{
map[i][f]=1;
}
}
for(i=1;i<=m;i++)
{
for(f=1;f<=n;f++)
{
map[i][f]=0;
}
}
scanf("%d",&num);
for(i=0;i<num;i++)
{
scanf("%d%d",&a,&b);
map[a+1][b+1]=1;
}
res=0;
max=0;
x=0;
y=0;
for(i=1;i<=m;i++)
{
for(f=1;f<=n;f++)
{
if(0==map[i][f])
{
map[i][f]=1;
if(0==map[i+dir[0][0]][f+dir[0][1]])
{
zan=0;
dfs(i,f,1,0);
if(zan>res)
{
res=zan;
max=0;
x=i;
y=f;
}
}
if(0==map[i+dir[3][0]][f+dir[3][1]])
{
zan=0;
dfs(i,f,1,3);
if(zan>res)
{
res=zan;
max=3;
x=i;
y=f;
}
}
if(0==map[i+dir[1][0]][f+dir[1][1]])
{
zan=0;
dfs(i,f,1,1);
if(zan>res)
{
res=zan;
max=1;
x=i;
y=f;
}
}
if(0==map[i+dir[2][0]][f+dir[2][1]])
{
zan=0;
dfs(i,f,1,2);
if(zan>res)
{
res=zan;
max=2;
x=i;
y=f;
}
}
map[i][f]=0;
}
}
}
printf("Case %d: %d %d %d ",ji,res,x-1,y-1);
if(0==max)
{
printf("E\n");
}
else if(1==max)
{
printf("S\n");
}
else if(2==max)
//.........这里部分代码省略.........
示例9: sumNumbers
int sumNumbers(TreeNode* root) {
int res=0;
dfs(root, res, 0);
return res;
}
示例10: longestConsecutive
int longestConsecutive(TreeNode* root) {
if(!root) return 0;
int cnt = root->val;
return max(dfs(root->left,1,cnt),dfs(root->right,1,cnt));
}
示例11: dfs
int Algorithms::dfs_shortest(GraphNode* a,GraphNode* b){
dfs(a);
return b->distance_till_here;
}
示例12: work
void work()
{
double others=0;
nleft=n;
dfs(1);
if(nleft) //检查是否存在最小树形图
{
printf("poor snoopy\n");
return;
}
memcpy(nowv,v,sizeof(v));
while(1)
{
ans=0;
memset(pre,0,sizeof(pre));
memset(tv,0,sizeof(tv));
memset(vis,0,sizeof(vis));
memset(next,0,sizeof(next));
memset(deal,0,sizeof(deal));
for(int i=2;i<=n;++i) if(!cut[i])
{
double mink=oo;
int minj=0;
for(int j=1;j<=n;++j) if(!cut[j])//求最小入边
if(nowv[j][i])
if(mink>nowv[j][i])
{
mink=nowv[j][i];
minj=j;
}
pre[i]=minj; //保存前驱后继
next[minj]=i;
ans+=mink;
}
int flag=0;
for(int i=1;i<=n;++i) if(!cut[i])
{
if(!vis[i]&&!deal[i])
{
memset(list,0,sizeof(list));
memset(tlist,0,sizeof(tlist));
memset(appear,0,sizeof(appear));
tlist[++tlist[0]]=i;
vis[i]=i;
int t=i,flagg=0;
while(pre[t]) // 寻找环
{
if(deal[pre[t]]) break;
if(vis[pre[t]]==i)
{
flagg=1;
break;
}
vis[pre[t]]=i;
tlist[++tlist[0]]=pre[t];
t=pre[t];
}
flag|=flagg;
if(flagg) //若有环,将i作为收缩后点的编号
{
int tmptt;
for(int j=1;j<=tlist[0];++j) if(tlist[j]==pre[t])
{
tmptt=j;
break;
}
for(int k=tmptt;k<=tlist[0];++k){ list[++list[0]]=tlist[k];appear[tlist[k]]=1;deal[tlist[k]]=1;}
others+=nowv[list[list[0]]][list[1]];
for(int j=1;j<list[0];++j)
others+=nowv[list[j]][list[j+1]];
memcpy(tv,nowv,sizeof(nowv));
for(int j=1;j<=list[0];++j) //先消除所有包含环内点的边
for(int k=1;k<=n;++k) if(!cut[k])
tv[list[j]][k]=tv[k][list[j]]=0;
for(int j=1;j<=list[0];++j)
{
int now=list[j];
double in=nowv[pre[list[j]]][list[j]];
for(int k=1;k<=n;++k) if(!cut[k])
{
if(!appear[k])
{
// printf("%d %d %d %.2f\n",now,k,list[1],in);
if(nowv[now][k]) //若为出边
if(tv[list[1]][k]==0||tv[list[1]][k]>nowv[now][k])
tv[list[1]][k]=nowv[now][k];
if(nowv[k][now]) //若为入边
if(tv[k][list[1]]==0||tv[k][list[1]]>nowv[now][k]-in)
tv[k][list[1]]=nowv[k][now]-in;
}
}
}
for(int j=2;j<=list[0];++j) cut[list[j]]=1;
}
}
}
if(!flag) //若无环
{
printf("%.2lf\n",ans+others);
//.........这里部分代码省略.........
示例13: max_flow
ll max_flow() {
gap[0] = n * m;
ll resu(0);
while (dis[1][1] < n * m) resu += dfs(1, 1, INFF);
return resu;
}
示例14: restoreIpAddresses
vector<string> restoreIpAddresses(string s) {
vector<string> ans;
dfs(0, 0, s, "", ans);
return ans;
}
示例15: dfs
double dfs(p,f){if(M[p][f]<0){
int i=0;
for(M[p][f]=0;i<n;i++)if(!(f&1<<i)&&(t=dfs(i,f|1<<i)+D[p]+D[i]-2*sqrt(D[p]*D[i]),M[p][f]<t))M[p][f]=t;
}return M[p][f];}