HDU-3103-The Heart of the Country(拓扑排序)

The Heart of the Country

Problem Description

The nation of Graphia is at war. The neighboring nations have for long watched in jealousy as Graphia erected prosperous cities and connected them with a network of highways. Now they want a piece of the pie.

Graphia consists of several cities, connected by highways. Graphian terrain is rough, so the only way to move between the cities is along the highways. Each city has a certain number of troops quartered there. Graphia’s military command knows that it will require a certain number of troops, K , to defend any city. They can defend a city with the troops stationed there, supported by the troops in any other city which is directly connected with a highway, with no cities in between. Any troops further away than that simply cannot get there in time. They also know that their enemies will onlyattack one city at a time – so the troops in a city can be used to defend that city, as well as any of its neighbors. However, if a city can’t be defended, then the military command must assume that the troops quartered in that city will be captured, and cannot aid in the defense of Graphia. In the case below, suppose K = 10 . City C might seem well defended, but it will eventually fall.

Graphia’s leadership wants to identify the Heart of their country – the largest possible group of cities that can mutually defend each other, even if all of the other cities fall.

More formally, a city is defensible if it can draw a total of at least K troops from itself, and from cities directly adjacent to it. A set of cities is defensible if every city in it is defensible, using only troops from itself and adjacent cities in that set. The Heart of the country is the largest possible defensible set of cities - that is, no other defensible set of cities has more cities in it.

Input

There will be several data sets. Each set begins with two integers, N and K , where N is the number of cities ( 3<=N<=1000 ), and K is the number of troops required to defend a city. The cities are numbered 0 through N - 1 .

On the next N lines are descriptions of the cities, starting with city 0. Each of the city description lines begins with an integer T , indicating the number of troops quartered in that city ( 0<=T<=10000 ). This is followed by an integer M , indicating the number of highways going out of that city, and then M integers, indicating the cities those highways go to. No two highways will go from and to the same cities, so every city in each list will be unique. No highway will loop from a city back to the same city. The highways go both ways, so that if city I is in city J 's list, then it’s guaranteed that city J will be in city I 's list in the input. The input will end with a line with two space-separated 0’s.

Output

For each data set, print two integers on a single line: The number of cities in the heart of the country, and the number of troops in the heart of the country. Print a space between the integers. There should be no blank lines between outputs.

Sample Input

4 900
100 2 1 2
200 2 0 3
500 2 0 3
1000 2 1 2
4 900
100 3 1 2 3
200 3 0 3 2
500 3 1 3 0
1000 3 2 1 0
0 0
 

Sample Output

3 1700
4 1800

解题思路:

交的人不多,我觉得大概率是不愿看题目。
看完题就可以发现是拓扑排序。
只不过条件不是入度为0。
可以把一个city和周边的troop加起来 等于这个点的入度。
当一个城市沦陷时。周边城市 减去这个城市的军队数量。
不足k的则沦陷,入队列。

AC代码:

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n,m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n,m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sc(n) scanf("%c",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
#define pii map<int,int>
#define mk make_pair
#define rtl rt<<1
#define rtr rt<<1|1
#define Max(x,y) (x)>(y)?(x):(y)
#define int long long


typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const ll mod = 10007;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
//const int inf = 0x3f3f3f3f;
inline int read(){int ret = 0, sgn = 1;char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-')sgn = -1;ch = getchar();}
while (ch >= '0' && ch <= '9'){ret = ret*10 + ch - '0';ch = getchar();}
return ret*sgn;}
inline void Out(int a){if(a>9) Out(a/10);putchar(a%10+'0');}
int qpow(int m, int k, int mod){int res=1%mod,t=m%mod;while(k){if(k&1)res=res*t%mod;t=t*t%mod;k>>=1;}return res;}
ll gcd(ll a,ll b){if(b > a) swap(a,b); return b==0?a : gcd(b,a%b);}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll x,ll mod){return qpow(x,mod-2,mod)%mod;}

const int N = 3e3+15;
struct node{
    int num;            // 自己军队数量
    vector<int> to;
}G[N];
int in[N];              // 军队数量
int lost[N];            // 沦陷标记

signed main()
{
    int n,m;
    while(cin>>n>>m && n+m){
        for(int i = 0 ; i < n ; i ++){
            G[i].num = 0;
            G[i].to.clear();
            lost[i] = 0;
            in[i] = 0;
        }
        for(int i = 0 ; i < n ; i ++){
            int x,y,k;
            cin>>x>>k;
            G[i].num += x;
            in[i] += x;
            for(int j = 0 ; j  < k ; j ++){
                cin>>y;
                in[y] += x;
                G[i].to.pb(y);
            }
        }
        queue<int> que;
        for(int i = 0 ; i < n ; i ++){
            if(in[i] < m){
                que.push(i);
                lost[i] = 1;
            }
        }
        while(!que.empty()){
            int x = que.front();que.pop();
            int nn = G[x].to.size();
            for(int i = 0 ;i < nn ; i ++){
                int to = G[x].to[i];
                if(!lost[to]){
                    in[to] -= G[x].num;
                    if(in[to] < m){
                        que.push(to);
                        lost[to] = 1;
                    }
                }
            }
        }
        int res1 = 0 ,res2 = 0;
        for(int i = 0 ; i < n ; i ++){
            if(!lost[i]){
                res1 += 1;
                res2 += G[i].num;
            }
        }
        cout<<res1<<" "<<res2<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值