zzulioj 1734: 堆 【dfs】

来源:互联网 发布:赵孟頫书法软件 编辑:程序博客网 时间:2024/05/22 03:05

题目复制不下来,就给个链接吧。

题目链接:http://acm.zzuli.edu.cn/problem.php?id=1734

 

当时去郑轻是我第一次去参赛,不知是不是没有勇气去实现自己的想法,这道题当时我的思路就是dfs,就是下面的代码。

现在做做一边ac了,真的很遗憾。

 

#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <vector>#include <algorithm>using namespace std;int value[110];vector<int > map[110];int n;int sum;//查询节点数 int vis[110];void getmap(){    int x, y;    int a, b;    for(int i = 1; i <= n; i++)    {        scanf("%d", &value[i]);        map[i].clear();    }    for(int i = 0;i < n-1; i++)    {        scanf("%d%d", &x, &y);        a = max(x,y);        b = min(x,y);        map[b].push_back(a);    }}void dfs(int node){    for(int i = 0; i < map[node].size(); i++)    {        int next = map[node][i];        if(!vis[next] && value[next] >= value[node])        {            vis[next] = 1;            sum++;            dfs(next);        }    }}int main(){    int t;    int i, j;    scanf("%d", &t);    while(t--)    {        scanf("%d", &n);        getmap();        memset(vis, 0, sizeof(vis));        sum = 1;        dfs(1);        if(sum == n)        printf("Yes\n");        else        printf("No\n");    }    return 0;}


 

 

0 0
原创粉丝点击