이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define INF 1000000000
#define LINF 1000000000000000LL
#define pb push_back
#define all(x) x.begin(), x.end()
#define len(s) (int)s.size()
#define test_case { int t; cin>>t; while(t--)solve(); }
#define input(n, v) {for(int i = 0;i<n;i++) cin>>v[i];}
#define output(n, v) {for(int i = 0;i<n;i++) cout<<v[i]<<" "; cout<<endl;}
#define single_case solve();
#define line cout<<"------------"<<endl;
#define ios { ios_base::sync_with_stdio(false); cin.tie(NULL); }
using namespace std;
int n;
const int N = 5e3 + 5;
vector<vector<int> > v(N);
vector<vector<int> > g(N);
bool vidjen[N];
ll dp[N];
ll dfs(int u, int pret)
{
ll a = 0;
ll ans = 0;
for(int x : g[u])
{
if(x==pret) continue;
ans += dfs(x, u);
a += dp[x];
}
dp[u] = a + 1LL;
return dp[u] + ans;
}
ll make_graph(int u)
{
memset(vidjen, false, sizeof(vidjen));
queue<int> q;
q.push(u);
for(int i = 0;i<N;i++)
g[i].clear();
vidjen[u] = 1;
while(len(q))
{
int top = q.front();
q.pop();
for(int x : v[top])
{
if(vidjen[x]) continue;
q.push(x);
g[top].pb(x);
vidjen[x] = 1;
}
}
for(int i = 0;i<N;i++)
dp[i] = 0;
for(int i = 0;i<n;i++)
if(!vidjen[i]) return LINF;
return dfs(u, -1);
}
bool cmp(int a, int b)
{
if(len(v[a])>len(v[b]))
return true;
else
return false;
}
int main()
{
ios
cin>>n;
for(int i = 0;i<n;i++)
{
int k;
cin>>k;
while(k--)
{
int a;
cin>>a;
a--;
v[a].pb(i);
}
}
vector<int> a;
for(int i = 0;i<n;i++)
a.pb(i);
for(int i = 0;i<n;i++)
sort(all(a), cmp);
ll res = make_graph(a[0]);
cout<<res;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |