Submission #39780

#TimeUsernameProblemLanguageResultExecution timeMemory
39780nonocutBosses (BOI16_bosses)C++14
100 / 100
761 ms2348 KiB
#include<bits/stdc++.h>
using namespace std;

#define ll long long
const int maxn = 5e3 + 5;
const long long inf = 1e15;

int n;
int par[maxn], q[maxn];
long long dp[maxn];
vector<int> way[maxn];

ll bfs(int u) {
    int i,l,r,x;
    memset(par,0,sizeof(par));
    memset(dp,0,sizeof(dp));

    l = 1; r = 0;
    par[u] = u; q[++r] = u;
	while(l<=r) {
        x = q[l++];
        for(auto y : way[x]) {
            if(!par[y]) par[y] = x, q[++r] = y;
        }
	}

	long long ret = 0;
	for(i=r;i>=1;i--) {
        x = q[i]; dp[x]++;
        if(i>1) dp[par[x]] += dp[x];
        ret += dp[x];
	}

	if(r<n) return inf;
	return ret;
}
int main() {
	int i,x,k;

	scanf("%d",&n);
	for(i=1;i<=n;i++) {
		scanf("%d",&k);
		while(k--) {
			scanf("%d",&x);
			way[x].push_back(i);
		}
	}

	ll ans = inf;
	for(i=1;i<=n;i++) ans = min(ans, bfs(i));
	printf("%lld",ans);
}

Compilation message (stderr)

bosses.cpp: In function 'int main()':
bosses.cpp:40:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
                ^
bosses.cpp:42:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&k);
                 ^
bosses.cpp:44:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d",&x);
                  ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...