集训队作业

C - Bank Hacking

Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search for Zane, he would need a lot of money, of which he sadly has none. To deal with the problem, he has decided to hack the banks.

There are n banks, numbered from 1 to n. There are also n - 1 wires connecting the banks. All banks are initially online. Each bank also has its initial strength: bank i has initial strength ai.

Let us define some keywords before we proceed. Bank i and bank j are neighboring if and only if there exists a wire directly connecting them. Bank i and bank j are semi-neighboring if and only if there exists an online bank k such that bank i and bank k are neighboring and bank k and bank j are neighboring.

When a bank is hacked, it becomes offline (and no longer online), and other banks that are neighboring or semi-neighboring to it have their strengths increased by 1.

To start his plan, Inzane will choose a bank to hack first. Indeed, the strength of such bank must not exceed the strength of his computer. After this, he will repeatedly choose some bank to hack next until all the banks are hacked, but he can continue to hack bank x if and only if all these conditions are met:

  1. Bank x is online. That is, bank x is not hacked yet.
  2. Bank x is neighboring to some offline bank.
  3. The strength of bank x is less than or equal to the strength of Inzane's computer.

Determine the minimum strength of the computer Inzane needs to hack all the banks.

Input

The first line contains one integer n (1 ≤ n ≤ 3·105) — the total number of banks.

The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the strengths of the banks.

Each of the next n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ nui ≠ vi) — meaning that there is a wire directly connecting banks ui and vi.

It is guaranteed that the wires connect the banks in such a way that Inzane can somehow hack all the banks using a computer with appropriate strength.

Output

Print one integer — the minimum strength of the computer Inzane needs to accomplish the goal.

题解:就是去判断最大的周围有几个权值比其小一的点,不妨记为Max和Max-1

如果Max-1都在Max(仅有一个)周围那么答案就是Max

如果Max都在其中一个Max-1周围那么答案就是Max+1

其余情况都是Max+2

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#include <string.h>
using namespace std;
typedef long long ll;
const int maxn=300005;
int a[maxn],visit[maxn];
vector<int> v[maxn];
 
int main() {
	int n,i,x,y,j,max1,ans,maxi;
	scanf("%d",&n);
	max1=-1<<30;
	if (n==1) {
		scanf("%d",&a[0]);
		cout << a[0];
		return 0;
	}
	for (i=1;i<=n;i++) {
		scanf("%d",&a[i]);
		if (a[i]>max1) {
			max1=a[i];
			maxi=i;
	    }
	}
	int nummax=0,nummax2=0;
	for (i=1;i<=n;i++) {
		if (a[i]==max1) nummax++;
		if (a[i]==max1-1) nummax2++;
	}
	ans=max1+2;
	for (i=1;i<=n-1;i++) {
		scanf("%d%d",&x,&y);
	    v[x].push_back(y);
	    v[y].push_back(x);
    }
    int num;
    if (nummax==1) {
    	num=0;
    	for (i=0;i<v[maxi].size();i++) 
    	    if (a[v[maxi][i]]==max1-1) num++;
        if (num==nummax2) {
        	ans-=2; 
            cout << ans;
            return 0;
        }
    }
    for (i=1;i<=n;i++) {
    	num=0;
    	if (a[i]==max1) num++;
    	for (j=0;j<v[i].size();j++) {
    		if (a[v[i][j]]==max1) num++;
    	}
    	if (num==nummax) {
    		ans--;
    		break;
    	}
    }
    cout << ans;
	return 0;
}

D - Police Stations

 

Inzane finally found Zane with a lot of money to spare, so they together decided to establish a country of their own.

Ruling a country is not an easy job. Thieves and terrorists are always ready to ruin the country's peace. To fight back, Zane and Inzane have enacted a very effective law: from each city it must be possible to reach a police station by traveling at most d kilometers along the roads.

There are n cities in the country, numbered from 1 to n, connected only by exactly n - 1 roads. All roads are 1 kilometer long. It is initially possible to travel from a city to any other city using these roads. The country also has k police stations located in some cities. In particular, the city's structure satisfies the requirement enforced by the previously mentioned law. Also note that there can be multiple police stations in one city.

However, Zane feels like having as many as n - 1 roads is unnecessary. The country is having financial issues, so it wants to minimize the road maintenance cost by shutting down as many roads as possible.

Help Zane find the maximum number of roads that can be shut down without breaking the law. Also, help him determine such roads.

Input

The first line contains three integers nk, and d (2 ≤ n ≤ 3·105, 1 ≤ k ≤ 3·105, 0 ≤ d ≤ n - 1) — the number of cities, the number of police stations, and the distance limitation in kilometers, respectively.

The second line contains k integers p1, p2, ..., pk (1 ≤ pi ≤ n) — each denoting the city each police station is located in.

The i-th of the following n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ nui ≠ vi) — the cities directly connected by the road with index i.

It is guaranteed that it is possible to travel from one city to any other city using only the roads. Also, it is possible from any city to reach a police station within d kilometers.

Output

In the first line, print one integer s that denotes the maximum number of roads that can be shut down.

In the second line, print s distinct integers, the indices of such roads, in any order.

If there are multiple answers, print any of them.

题解: 分别以所有的警察局为头结点开始bfs,到达没有到达的节点,最后没有用过的边就是可以去掉的边

并且本题中保证了所有城市可以到达也就是一个最小生成树了,所以d是没有用处的

#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
using namespace std;
struct node
{
    int to,pos;
}now,nex;
vector<node >mp[300800];
int vis[300800];
int ans[300800];
int main()
{
    int n,m,k;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        queue<int >s;
        memset(vis,0,sizeof(vis));
        memset(ans,0,sizeof(ans));
        for(int i=1;i<=n;i++)mp[i].clear();
        for(int i=0;i<m;i++)
        {
            int x;
            scanf("%d",&x);
            s.push(x);
            vis[x]=1;
        }
        for(int i=1;i<=n-1;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            now.to=y;
            now.pos=i;
            mp[x].push_back(now);
            now.to=x;
            now.pos=i;
            mp[y].push_back(now);
        }
        while(!s.empty())
        {
            int u=s.front();
            s.pop();
            for(int i=0;i<mp[u].size();i++)
            {
                int v=mp[u][i].to;
                if(vis[v]==0)
                {
                    ans[mp[u][i].pos]=1;
                    vis[v]=1;
                    s.push(v);
                }
            }
        }
        int cnt=0;
        for(int i=1;i<=n-1;i++)if(ans[i]==0)cnt++;
        printf("%d\n",cnt);
        for(int i=1;i<=n-1;i++)
        {
            if(ans[i]==0)printf("%d ",i);
        }
        printf("\n");
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值