#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
vector<vector<pair<int,int> > > adj;
vector<bool> visited,in;
vector<int> cmp,st;
queue<int> q;
priority_queue<pair<int,pair<int,int> >,vector<pair<int,pair<int,int> > >,greater<pair<int,pair<int,int> > > > pq;
int solve(int x,int n)
{
int sz = 0;
q.push(x);
visited[x] = true;
in.assign(n,false);
while(!q.empty())
{
int p = q.front();
q.pop();
sz++;
in[p] = true;
for(int i = 0;i < adj[p].size();i++) if(!visited[adj[p][i].second]){ visited[adj[p][i].second] = true; q.push(adj[p][i].second); }
}
while(!pq.empty()) pq.pop();
for(int i = 0;i < n;i++) if(in[i] and adj[i].size()<=1){ pq.push({0,{i,i}}); st[i] = 1; }
if(sz==1) return 0;
int ans;
while(true)
{
int w = pq.top().first,p = pq.top().second.first,pr = pq.top().second.second;
pq.pop();
if(p!=pr) st[p]+=st[pr];
if(st[p]==sz){ ans = w; break; }
for(int i = 0;i < adj[p].size();i++) if(adj[p][i].second!=pr) pq.push({adj[p][i].first+w,{adj[p][i].second,p}});
}
return ans;
}
int travelTime(int n,int m,int l,int a[],int b[],int t[])
{
adj.resize(n);
visited.resize(n);
for(int i = 0;i < n;i++)
{
adj[a[i]].push_back({t[i],b[i]});
adj[b[i]].push_back({t[i],a[i]});
}
for(int i = 0;i < n;i++)
{
if(visited[i]) continue;
cmp.push_back(solve(i,n));
}
sort(cmp.begin(),cmp.end());
reverse(cmp.begin(),cmp.end());
if(cmp.size()==1) return cmp[0];
else if(cmp.size()==2) return cmp[0]+cmp[1]+l;
else return max(cmp[0]+cmp[1]+l,cmp[1]+cmp[2]+l+l);
}
Compilation message
dreaming.cpp: In function 'int solve(int, int)':
dreaming.cpp:23:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i < adj[p].size();i++) if(!visited[adj[p][i].second]){ visited[adj[p][i].second] = true; q.push(adj[p][i].second); }
~~^~~~~~~~~~~~~~~
dreaming.cpp:35:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i < adj[p].size();i++) if(adj[p][i].second!=pr) pq.push({adj[p][i].first+w,{adj[p][i].second,p}});
~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
57 ms |
12664 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
57 ms |
12664 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
57 ms |
12664 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
25 ms |
11768 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
57 ms |
12664 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
57 ms |
12664 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |