Submission #891976

# Submission time Handle Problem Language Result Execution time Memory
891976 2023-12-24T14:21:30 Z abcvuitunggio Towns (IOI15_towns) C++17
100 / 100
12 ms 860 KB
#include "towns.h"
#include <bits/stdc++.h>
using namespace std;
int n,dist[111][111],u,v,R,sz[111],p[111],cnt;
int d(int u, int v){
    if (u==v)
        return 0;
    if (u>v)
        swap(u,v);
    if (dist[u][v])
        return dist[u][v];
    cnt++;
    return dist[u][v]=getDistance(u,v);
}
int f(int i){
    return (p[i]==i?i:p[i]=f(p[i]));
}
void unite(int i, int j){
    j=f(j);
    i=f(i);
    if (i==j)
        return;
    sz[i]+=sz[j];
    p[j]=i;
}
int calc(int i){
    return d(i,u)-d(i,0)+d(0,u);
}
bool check(int i, int j){
    return (d(u,i)+d(u,j)-d(i,j)>calc(i));
}
int check(int i){
    vector <int> a;
    for (int j=1;j<=n;j++)
        if (calc(j)==i)
            a.push_back(j);
    stack <int> st,st2;
    for (int i:a){
        if (st.empty()||!check(st.top(),i)){
            st.push(i);
            if (!st2.empty()){
                st.push(st2.top());
                st2.pop();
            }
        }
        else
            st2.push(i);
    }
    int t=st.top(),cnt=st2.size();
    while (!st.empty()){
        int u=st.top();
        if (check(u,t)){
            cnt++;
            if (st.size()==1){
                st2.push(st.top());
                st.pop();
            }
            else{
                st.pop();
                st.pop();
            }
        }
        else{
            if (st2.empty())
                return 1;
            st.pop();
            st2.pop();
        }
    }
    return (cnt>n/2?-1:1);
}
int hubDistance(int N, int sub){
    cnt=0;
    n=N;
    memset(dist,0,sizeof(dist));
    pair <int, int> mx={0,-1};
    for (int i=0;i<n;i++)
        mx=max(mx,{d(0,i),i});
    u=mx.second;
    mx={0,-1};
    for (int i=0;i<n;i++)
        mx=max(mx,{d(u,i),i});
    v=mx.second,R=1e9,sz[0]=sz[1]=0;
    vector <int> ve;
	for (int i=0;i<n;i++){
        ve.push_back(calc(i));
        R=min(R,abs(d(u,v)-calc(i)));
	}
    sort(ve.begin(),ve.end());
    int ch=-1;
    vector <int> candidates={d(u,v)-R};
    if (R)
        candidates.push_back(d(u,v)+R);
    for (int i:candidates)
        if (i==ve[n/2]||i==ve[(n-1)/2])
            ch=max(ch,check(i));
    return (R+d(u,v))/2*ch;
}

Compilation message

towns.cpp: In function 'int d(int, int)':
towns.cpp:5:18: warning: declaration of 'v' shadows a global declaration [-Wshadow]
    5 | int d(int u, int v){
      |              ~~~~^
towns.cpp:4:24: note: shadowed declaration is here
    4 | int n,dist[111][111],u,v,R,sz[111],p[111],cnt;
      |                        ^
towns.cpp:5:11: warning: declaration of 'u' shadows a global declaration [-Wshadow]
    5 | int d(int u, int v){
      |       ~~~~^
towns.cpp:4:22: note: shadowed declaration is here
    4 | int n,dist[111][111],u,v,R,sz[111],p[111],cnt;
      |                      ^
towns.cpp: In function 'int check(int)':
towns.cpp:38:14: warning: declaration of 'int i' shadows a parameter [-Wshadow]
   38 |     for (int i:a){
      |              ^
towns.cpp:32:15: note: shadowed declaration is here
   32 | int check(int i){
      |           ~~~~^
towns.cpp:49:20: warning: declaration of 'cnt' shadows a global declaration [-Wshadow]
   49 |     int t=st.top(),cnt=st2.size();
      |                    ^~~
towns.cpp:4:43: note: shadowed declaration is here
    4 | int n,dist[111][111],u,v,R,sz[111],p[111],cnt;
      |                                           ^~~
towns.cpp:49:32: warning: conversion from 'std::stack<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   49 |     int t=st.top(),cnt=st2.size();
      |                        ~~~~~~~~^~
towns.cpp:51:13: warning: declaration of 'u' shadows a global declaration [-Wshadow]
   51 |         int u=st.top();
      |             ^
towns.cpp:4:22: note: shadowed declaration is here
    4 | int n,dist[111][111],u,v,R,sz[111],p[111],cnt;
      |                      ^
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:72:28: warning: unused parameter 'sub' [-Wunused-parameter]
   72 | int hubDistance(int N, int sub){
      |                        ~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 344 KB Output is correct
2 Correct 9 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 12 ms 348 KB Output is correct
5 Correct 11 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 348 KB Output is correct
2 Correct 9 ms 856 KB Output is correct
3 Correct 12 ms 860 KB Output is correct
4 Correct 12 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 348 KB Output is correct
2 Correct 12 ms 536 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 12 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 344 KB Output is correct
2 Correct 12 ms 344 KB Output is correct
3 Correct 12 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 348 KB Output is correct
2 Correct 12 ms 860 KB Output is correct
3 Correct 12 ms 860 KB Output is correct