zzulioj 1734 堆

比赛的时候不会写,想不到DFS,一直以为需要二叉树或者建堆什么的,也没学,后来才明白这个题

代码:

#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;  
}  

猜你喜欢

转载自www.cnblogs.com/nr1999/p/8903067.html
今日推荐