int maxPathSum(TreeNode *root) {
     max_sum = INT_MIN;
     dfs(root);
     return max_sum;
 }
Ejemplo n.º 2
0
 NestedIterator(vector<NestedInteger> &nestedList) {
     index = 0;
     dfs(nestedList);
 }
Ejemplo n.º 3
0
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;
}
Ejemplo n.º 4
0
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);
}
Ejemplo n.º 5
0
 bool hasPathSum(TreeNode *root, int sum) {
     if(!root) return false;
     return dfs(root, sum, 0);
 }
 TreeNode* bstFromPreorder(vector<int>& preorder) {
     int i = 0;
     return dfs(preorder, i, INT_MAX);
 }
Ejemplo n.º 7
0
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];
}
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)
        {
            printf("W\n");
        }
        else
        {
            printf("N\n");
        }
    }
    return 0;
}
Ejemplo n.º 9
0
 int sumNumbers(TreeNode* root) {
     int res=0;
     dfs(root, res, 0);
     return res;
 }
 int longestConsecutive(TreeNode* root) {
     if(!root) return 0;
     int cnt = root->val;
     return max(dfs(root->left,1,cnt),dfs(root->right,1,cnt));
 }
int Algorithms::dfs_shortest(GraphNode* a,GraphNode* b){
	dfs(a);
	return b->distance_till_here;
}
Ejemplo n.º 12
0
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);
			return;
		}
		else memcpy(nowv,tv,sizeof(tv));
	}
}
Ejemplo n.º 13
0
Archivo: dhl.cpp Proyecto: cjsoft/noip
ll max_flow() {
    gap[0] = n * m;
    ll resu(0);
    while (dis[1][1] < n * m) resu += dfs(1, 1, INFF);
    return resu;
}
Ejemplo n.º 14
0
		vector<string> restoreIpAddresses(string s) {
			vector<string> ans;
			dfs(0, 0, s, "", ans);
			return ans;
		}
Ejemplo n.º 15
0
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];}
Ejemplo n.º 16
0
 TreeNode* invertTree(TreeNode* root) {
     if(root==NULL)
         return NULL;
     dfs(root);
     return root;
 }
Ejemplo n.º 17
0
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);}
 int diameterOfBinaryTree(TreeNode* root) {
     dfs(root);
     return res;
 }
Ejemplo n.º 19
0
 vector<vector<int>> permute(vector<int>& nums) {
     sort(nums.begin(), nums.end());
     dfs(0, nums);
     return resList;
 }
Ejemplo n.º 20
0
 vector<vector<int> > combine(int n, int k) {
     vector<vector<int> > result;
     vector<int> path;
     dfs(n, k, 1, 0, path, result);
     return result;
 }
Ejemplo n.º 21
0
void dfs(int x)
{
    if(x!=1)
        dfs(path[x]);
    printf("%d\n",x);
}
Ejemplo n.º 22
0
 int rob(TreeNode* root) {
     pair<int,int> res = dfs(root);
     return std::max(res.first,res.second);
 }
Ejemplo n.º 23
0
int main() {
    freopen("4-1.txt", "r", stdin);
    scanf("%d",&n);
    dfs(1);
    return 0;
}
Ejemplo n.º 24
0
int main(){
    freopen("chem.in", "r", stdin);
    freopen("chem.out", "w", stdout);
    //Initialize
    memset(h, 0, sizeof(h));
    h[0] = 1;
    getchar(); getchar();
    dfsread(0);
    //1st BFS
    qh = qt = que[0] = 0;
    memset(inq, 0, sizeof(inq));
    inq[0] = 1;
    while(qh <= qt){
        for(std::vector<int>::iterator it = e[que[qh]].begin(); it != e[que[qh]].end(); ++it) if(!inq[*it]){
            inq[*it] = 1; que[++qt] = *it;
        }
        ++qh;
    }
    //2nd BFS
    que[0] = que[qt]; dist[que[qt]] = 0;
    qh = qt = 0;
    memset(inq, 0, sizeof(inq));
    inq[que[qh]] = 1;
    while(qh <= qt){
        for(std::vector<int>::iterator it = e[que[qh]].begin(); it != e[que[qh]].end(); ++it) if(!inq[*it]){
            inq[*it] = 1; que[++qt] = *it;
            pred[*it] = que[qh];
            dist[*it] = dist[que[qh]] + 1;
        }
        ++qh;
    }
    //Find Center
    int cen;
    if(dist[que[qt]] % 2){
        int c = que[qt], tt = dist[que[qt]] / 2;
        while(tt--) c = pred[c];
        int d = pred[c]; ++n;
        for(std::vector<int>::iterator it = e[c].begin(); it != e[c].end(); ++it) if(*it == d){
            e[c].erase(it); break;
        }
        for(std::vector<int>::iterator it = e[d].begin(); it != e[d].end(); ++it) if(*it == c){
            e[d].erase(it); break;
        }
        e[c].push_back(n);
        e[d].push_back(n);
        e[n].push_back(c);
        e[n].push_back(d);
        cen = n;
    }else{
        int c = que[qt], tt = dist[que[qt]] / 2;
        while(tt--) c = pred[c];
        cen = c;
    }
    //DFS
    for(int i = 0; i <= n; ++i){
        l[i].l = 1; r[i].l = 0;
        l[i].next = r[i].next = NULL;
    }
    memset(inq, 0, sizeof(inq));
    dfs(cen);
    printf("%d\n", ans[cen]);
    return 0;
}
Ejemplo n.º 25
0
 int totalNQueens(int n) {
     vector<int> path(n);
     dfs(path, 0, n);
     return result;
 }
Ejemplo n.º 26
0
  // solution1: dfs  timeout (up - bottom)
  int minimumTotal_1(vector<vector<int>>& triangle){
    int res = INT_MAX;  // Because we need minimum, so we need compare

    dfs(triangle, res, 0, 0, 0);
    return res;
  }
Ejemplo n.º 27
0
int main()
{
     //freopen("1.txt", "r", stdin);

     int cases;
     scanf("%d", &cases);

    while (cases--)
    {
        memset(g, 0, sizeof(g));
        memset(din, 0, sizeof(din));
        memset(dout, 0, sizeof(dout));
        memset(visit, 0, sizeof(visit));

        int word_num;
        scanf("%d", &word_num);

        for (int i = 0; i < word_num; i++)
        {
            scanf("%s", word);
            int t = strlen(word);
            g[word[0]][word[t-1]]++;
            dout[word[0]]++;
            din[word[t-1]]++;
        }

        // 判断基数
        int odd_start = 0;
        int start = 0;
        int end = 0; // 计算结尾的点,不可重复
        int letter_num = 0; // 字母数量,dfs必须遍历这个数量的字母
        bool error = false;
        for (char c = 'a'; c <= 'z'; c++)
        {
            if (din[c] || dout[c])
            {
                if (dout[c]-din[c] > 0)
                {
                    letter_num += dout[c];
                    visit[c] = dout[c];
                }
                else
                {
                    letter_num += din[c];
                    visit[c] = din[c];
                }

                // 如果是环的话,那么随便选择一个出度大于0的
                if (dout[c])
                    start = c;

                if ((dout[c]-din[c]) == 1)
                {
                    if (odd_start != 0)
                    {
                        error = true;
                        break;
                    }
                    odd_start = c;
                }
                else if ((din[c]-dout[c]) == 1)
                {
                    if (end != 0)
                    {
                        error = true;
                        break;
                    }
                    end = c;
                }
                else
                {
                    if (din[c]-dout[c] != 0)
                    {
                        error = true;
                        break;
                    }
                }
            }
        }

        if (error)
        {
            printf("The door cannot be opened.\n");
            continue;
        }

        //是否遍历了所有字母
        if (letter_num != dfs(odd_start==0?start:odd_start))
        {
            printf("The door cannot be opened.\n");
            continue;
        }

        printf("Ordering is possible.\n");
    }

    return 0;
}
 int maxPathSum(TreeNode* root) {
     int max_value = INT_MIN;
     dfs(root,max_value);
     return max_value;
 }
 void debug()
 {
     printf("Root == %d\n",rt);
     dfs(rt);
 }
Ejemplo n.º 30
0
void solve() {
	if(dfs(0,0)) printf("Yes\n");
	else printf("No\n");
}