Submission #290702

# Submission time Handle Problem Language Result Execution time Memory
290702 2020-09-04T10:46:36 Z davooddkareshki Bosses (BOI16_bosses) C++17
100 / 100
752 ms 760 KB
#include<bits/stdc++.h>

using namespace std;

#define int long long
#define F first
#define S second
#define pii pair<int,int>
#define mpr make_pair

typedef long long ll;

const int maxn = 5010;
const int mod = 1e9+7;
const int inf = 1e18+10;

int n, m;
vector<int> g[maxn];

bool mark[maxn];
int dis[maxn];
int bfs(int s)
{
    memset(mark, 0, sizeof mark);
    memset(dis, 0, sizeof dis);

    queue<int> q;
    q.push(s); dis[s] = 1; mark[s] = 1;
    int ans = 0;

    while(q.size())
    {
        int v = q.front();
        q.pop();
        ans += dis[v];

        for(auto u : g[v])
            if(!mark[u])
            {
                mark[u] = 1;
                dis[u] = dis[v] + 1;
                q.push(u);
            }
    }
    for(int i = 1; i <= n; i++) if(!mark[i]) return inf;
    return ans;
}

signed main()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	cin>> n;
	for(int i = 1, sz; i <= n; i++)
	{
        cin>> sz;
        while(sz--)
        {
            int v; cin>> v;
            g[v].push_back(i);
        }
	}

	int ans = inf;
	for(int i = 1; i <= n; i++)
        ans = min(ans,bfs(i));
    cout<< ans;
}
/*
4
1 4
3 1 3 4
2 1 2
1 3
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 0 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 0 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 0 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 5 ms 640 KB Output is correct
13 Correct 5 ms 672 KB Output is correct
14 Correct 134 ms 632 KB Output is correct
15 Correct 11 ms 640 KB Output is correct
16 Correct 579 ms 728 KB Output is correct
17 Correct 737 ms 760 KB Output is correct
18 Correct 752 ms 760 KB Output is correct