Subordinates

Subordinates
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n workers in a company, each of them has a unique id from 1 to nExaclty one of them is a chief, his id is s. Each worker except the chief has exactly one immediate superior.

There was a request to each of the workers to tell how how many superiors (not only immediate). Worker's superiors are his immediate superior, the immediate superior of the his immediate superior, and so on. For example, if there are three workers in the company, from which the first is the chief, the second worker's immediate superior is the first, the third worker's immediate superior is the second, then the third worker has two superiors, one of them is immediate and one not immediate. The chief is a superior to all the workers except himself.

Some of the workers were in a hurry and made a mistake. You are to find the minimum number of workers that could make a mistake.

Input

The first line contains two positive integers n and s (1 ≤ n ≤ 2·105, 1 ≤ s ≤ n) — the number of workers and the id of the chief.

The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ n - 1), where ai is the number of superiors (not only immediate) the worker with id i reported about.

Output

Print the minimum number of workers that could make a mistake.

Examples
input
3 2
2 0 2
output
1
input
5 3
1 0 0 4 1
output
2
Note

In the first example it is possible that only the first worker made a mistake. Then:

  • the immediate superior of the first worker is the second worker,
  • the immediate superior of the third worker is the first worker,
  • the second worker is the chief.
    分析:上下级关系是一棵树,所以枚举直径取最优情况即可;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <unordered_map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    #define sys system("pause")
    #define intxt freopen("in.txt","r",stdin)
    const int maxn=2e5+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,p[maxn],cnt,ans,num,ok;
    int main()
    {
        int i,j;
        ans=1e9;
        scanf("%d%d",&n,&m);
        if(n==1)return 0*puts("0");
        rep(i,1,n)
        {
            scanf("%d",&j);
            if(i!=m)p[j]++;
            else
            {
                if(j!=0)cnt++;
            }
        }
        rep(i,1,n-1)
        {
            num+=p[i];
            if(!p[i])
            {
                ok++;
            }
            ans=min(ans,n-1-num+max(0,ok-n+num+1));
        }
        printf("%d
    ",ans+cnt);
        //system("Pause");
        return 0;
    }
原文地址:https://www.cnblogs.com/dyzll/p/6095636.html