# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
171667 | DavidDamian | Islands (IOI08_islands) | C++11 | 2073 ms | 131076 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int n;
struct edge{
int from,to;
ll w;
};
vector<edge> adjList[1000006];
int color[1000006];
int p[1000006];
int weight[1000006];
vector<edge> cycle;
void dfsVisit(int u)
{
color[u]=1;
for(edge e: adjList[u]){
int v=e.to;
if(v==p[u] && e.w==weight[u]) continue;
if(color[v]==0){
p[v]=u;
weight[v]=e.w;
dfsVisit(v);
}
else if(color[v]==1){ //Cycle
edge x=e;
swap(x.from,x.to);
cycle.push_back(x);
swap(x.from,x.to);
while(x.from!=v){
x.to=p[x.from];
x.w=weight[x.from];
cycle.push_back(x);
x.from=x.to;
}
}
}
color[u]=-1;
}
queue<int> Q;
ll d[1000006];
int c=1;
ll diameter(int r)
{
color[r]=c;
d[r]=0;
Q.push(r);
ll maximum=0;
int node=r;
while(Q.size()){
int u=Q.front();
Q.pop();
for(edge e: adjList[u]){
int v=e.to;
if(v==0) continue;
if(color[v]!=c){
color[v]=c;
d[v]=d[u]+e.w;
if(d[v]>maximum && v!=0){
maximum=d[v];
node=v;
}
Q.push(v);
}
}
}
color[node]=++c;
d[node]=0;
maximum=0;
Q.push(node);
while(Q.size()){
int u=Q.front();
Q.pop();
for(edge e: adjList[u]){
int v=e.to;
if(v==0) continue;
if(color[v]!=c){
color[v]=c;
d[v]=d[u]+e.w;
if(d[v]>maximum){
maximum=d[v];
node=v;
}
Q.push(v);
}
}
}
ll ans=d[node];
return ans;
}
ll total=0;
ll maxPath(int root)
{
ll maximum=0;
for(edge e: cycle){
int u=e.from,v=e.to;
int id1,id2;
for(int i=0;i<adjList[u].size();i++){
if(adjList[u][i].to==v && adjList[u][i].w==e.w){
id1=i;
adjList[u][i].to=0;
break;
}
}
for(int i=0;i<adjList[v].size();i++){
if(adjList[v][i].to==u && adjList[v][i].w==e.w){
id2=i;
adjList[v][i].to=0;
break;
}
}
++c;
maximum=max(maximum,diameter(root));
adjList[u][id1].to=v;
adjList[v][id2].to=u;
}
return maximum;
}
void dfs()
{
for(int i=1;i<=n;i++){
if(color[i]==0){
dfsVisit(i);
total+=maxPath(i);
cycle.clear();
}
}
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
ll a,b;
cin>>a>>b;
edge e={i,a,b};
adjList[e.from].push_back(e);
swap(e.from,e.to);
adjList[e.from].push_back(e);
}
dfs();
cout<<total<<'\n';
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |