本文整理汇总了C++中dfs函数的典型用法代码示例。如果您正苦于以下问题:C++ dfs函数的具体用法?C++ dfs怎么用?C++ dfs使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了dfs函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: maxPathSum
int maxPathSum(TreeNode *root) {
max_sum = INT_MIN;
dfs(root);
return max_sum;
}
开发者ID:BenjaminUJun,项目名称:algorithm-essentials,代码行数:5,代码来源:binary-tree-maximum-path-sum.cpp
示例2: NestedIterator
NestedIterator(vector<NestedInteger> &nestedList) {
index = 0;
dfs(nestedList);
}
开发者ID:roneilPMH,项目名称:LeetCode,代码行数:4,代码来源:341.cpp
示例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;
}
开发者ID:p473lr,项目名称:i-urge-mafia-gear,代码行数:79,代码来源:family.cpp
示例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);
}
开发者ID:imVivekGupta,项目名称:hello-world,代码行数:5,代码来源:dfs.c
示例5: hasPathSum
bool hasPathSum(TreeNode *root, int sum) {
if(!root) return false;
return dfs(root, sum, 0);
}
开发者ID:lccl7,项目名称:My-leetcode,代码行数:4,代码来源:PathSum.cpp
示例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];
}
开发者ID:zjwzcn07,项目名称:ACMLibrary,代码行数:6,代码来源:最近公共祖先LCA.cpp
示例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)
//.........这里部分代码省略.........
开发者ID:LinKin-22,项目名称:acm-algorithm,代码行数:101,代码来源:2782+2012-01-25+15+15+50.cpp
示例9: sumNumbers
int sumNumbers(TreeNode* root) {
int res=0;
dfs(root, res, 0);
return res;
}
开发者ID:yqliving,项目名称:leetcode,代码行数:5,代码来源:solution.cpp
示例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));
}
开发者ID:FeibHwang,项目名称:OJ-Leetcode,代码行数:5,代码来源:298_Binary_Tree_Longest_Consecutive_Sequence.cpp
示例11: dfs
int Algorithms::dfs_shortest(GraphNode* a,GraphNode* b){
dfs(a);
return b->distance_till_here;
}
开发者ID:devanshdalal,项目名称:The_Social_Network_Simulator_And_Analyzer,代码行数:4,代码来源:algorithms.cpp
示例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);
//.........这里部分代码省略.........
开发者ID:dementrock,项目名称:acm,代码行数:101,代码来源:7078292_WA.cpp
示例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;
}
开发者ID:cjsoft,项目名称:noip,代码行数:6,代码来源:dhl.cpp
示例14: restoreIpAddresses
vector<string> restoreIpAddresses(string s) {
vector<string> ans;
dfs(0, 0, s, "", ans);
return ans;
}
开发者ID:JeraKrs,项目名称:ACM,代码行数:5,代码来源:leetcode093.cpp
示例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];}
开发者ID:AbhiNki,项目名称:procon,代码行数:4,代码来源:tyama_aizu0120.c
示例16: invertTree
TreeNode* invertTree(TreeNode* root) {
if(root==NULL)
return NULL;
dfs(root);
return root;
}
开发者ID:lylalala,项目名称:leetcode,代码行数:6,代码来源:226_Invert_Binary_Tree.cpp
示例17: main
main(l,i,j,d){for(;~scanf("%d",&l);puts(d-r>l?"NA":"OK")){
for(d=n=0;getchar()-10;d+=D[n++]*2)scanf("%d",D+n);
for(i=0;i<n;i++)for(j=0;j<1<<n;j++)M[i][j]=-1;
for(r=i=0;i<n;)t=dfs(i++,1<<i),r=r>t?r:t;
}exit(0);}
开发者ID:AbhiNki,项目名称:procon,代码行数:5,代码来源:tyama_aizu0120.c
示例18: diameterOfBinaryTree
int diameterOfBinaryTree(TreeNode* root) {
dfs(root);
return res;
}
开发者ID:csxuejin,项目名称:LeetcodeCpp,代码行数:4,代码来源:543-diameter-of-binary-tree.cpp
示例19: permute
vector<vector<int>> permute(vector<int>& nums) {
sort(nums.begin(), nums.end());
dfs(0, nums);
return resList;
}
开发者ID:imAArtist,项目名称:simIr,代码行数:5,代码来源:code_329.cpp
示例20: combine
vector<vector<int> > combine(int n, int k) {
vector<vector<int> > result;
vector<int> path;
dfs(n, k, 1, 0, path, result);
return result;
}
开发者ID:cat715,项目名称:LeetCode,代码行数:6,代码来源:Combinations.cpp
注:本文中的dfs函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论