답안 #414019

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
414019 2021-05-29T18:48:01 Z HediChehaidar 꿈 (IOI13_dreaming) C++17
14 / 100
158 ms 28980 KB
/*
ID: hedichehaidar
TASK: photo
LANG: C++11
*/
#include<bits/stdc++.h>
#include"dreaming.h"
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (PGCD)
ll lcm(ll a , ll b) {return (a * b) / gcd(a , b);} // least common multiple (PPCM)
#define ss second
#define ff first
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define vi vector<int>
#define vii vector<pair<int,int>>
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd  pair<double,double>
#define vdd  vector<pdd>
#define dte  tuple<double , double , double>
using namespace std;
const int INF = 1000*1000*1000; // 1 e 9
const int MOD = INF + 7;
const double EPS = 0.000000001; // 1 e -9
const ll inf = (ll)1e18;

int n , l;
vii adj[(int)1e5 + 10];
map<int,int> m[(int)1e5 + 10];
pii down[(int)1e5 + 10];
int top[(int)1e5 + 10];
int dp[(int)1e5 + 10];
bool vis[(int)1e5 + 10];
bool p[(int)1e5 + 10];
int dist[(int)1e5 + 10];
vii v;
vi comp;
void dfs(int pos , int par){
    int a = 0 , b = 0;
    p[pos] = par;
    vis[pos] = true;
    for(auto c : adj[pos]){
        if(c.ff != par){
            dfs(c.ff , pos);
            if(c.ss + down[c.ff].ff >= a){
                swap(a , b);
                a = c.ss + down[c.ff].ff;
            }
            else b = max(b , down[c.ff].ff  + c.ss);
        }
    }
    down[pos] = {a , b};
}
void dfs2(int pos , int par){
    vis[pos] = true;
    comp.pb(pos);
    for(auto c : adj[pos]) if(c.ff != par) dfs2(c.ff , pos);
}
void solve(int pos , int par){
    vis[pos] = true;
    if(par == -1)  top[pos] = 0;
    else{
        top[pos] = m[pos][par] + top[par];
        if(down[par].ff == m[pos][par] + down[pos].ff) top[pos] = max(top[pos] , m[pos][par] + down[par].ss);
        else top[pos] = max(m[pos][par] + down[par].ff , top[pos]);
    }
    for(auto c : adj[pos]) if(c.ff != par) solve(c.ff , pos);
    dp[pos] = max(top[pos] , down[pos].ff);
}
int travelTime(int N , int M , int L , int a[] , int b[] , int t[]){
    n = N; l = L;
    for(int i = 0 ; i < M ; i++){
        adj[a[i]].pb({b[i] , t[i]});
        adj[b[i]].pb({a[i] , t[i]});
        m[a[i]][b[i]] = t[i];
        m[b[i]][a[i]] = t[i];
    }
    for(int i = 0 ; i < n ; i++){
        if(!vis[i]) dfs(i , -1);
    }
    memset(vis , 0 , sizeof vis);
    for(int i = 0 ; i < n ; i++){
        if(!vis[i]) solve(i , -1);
    }
    memset(vis , 0 , sizeof vis);
    for(int i = 0 ; i < n ; i++){
        if(!vis[i]){
            dfs2(i , -1);
            int mn = 0;
            for(int j = 1 ; j < comp.size() ; j++) if(dp[comp[j]] < dp[comp[mn]]) mn = j;
            v.pb({dp[comp[mn]] , comp[mn]});
            //for(auto c : comp) cout << c << " " << dp[c] << "    "; cout << "\n";
            comp.clear();
        }
    }
    sort(all(v));
   // for(auto c : v) cout << c.ff << " " << c.ss << "   "; cout << "\n";
    for(int i = 0 ; i < v.size() - 1 ; i++){
        adj[v.back().ss].pb({v[i].ss , l});
        adj[v[i].ss].pb({v.back().ss , l});
    }
    queue<int> q;
    memset(vis , 0 , sizeof vis);
    q.push(0);
    while(!q.empty()){
        int x = q.front(); q.pop();
        for(auto c : adj[x]){
            if(!vis[c.ff]) {
                dist[c.ff] = dist[x] + c.ss;
                vis[c.ff] = true;
                q.push(c.ff);
            }
        }
    }
    int mx = 0;
    for(int i = 1 ; i < n ; i++) if(dist[i] > dist[mx]) mx = i;
    q.push(mx);
    memset(vis , 0 , sizeof vis);
    memset(dist , 0 , sizeof dist);
    while(!q.empty()){
        int x = q.front(); q.pop();
        for(auto c : adj[x]){
            if(!vis[c.ff]) {
                dist[c.ff] = dist[x] + c.ss;
                vis[c.ff] = true;
                q.push(c.ff);
            }
        }
    }
    int res = 0;
    for(int i = 0 ; i < n ; i++) res = max(res , dist[i]);
    return res;
}
/*int main() {
    //ifstream fin ("race.in");
    //ofstream fout ("race.out");
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int N , M , L ;
    int A[(int)1e5 + 10];
    int B[(int)1e5 + 10];
    int T[(int)1e5 + 10];
    cin>>N>>M>>L;
    for(int i = 0 ; i < M ; i++) cin>>A[i]>>B[i]>>T[i];
    cout << travelTime(N , M , L , A , B , T);
    return 0;
}*/
/*
12
8
2
0 8 4
8 2 2
2 7 4
5 11 3
5 1 7
1 3 1
1 9 5
10 6 3
*/
/*
    Think of : BS / DFS / BFS / SSSP / SCC / MSP / MAX FLOW / TOPSORT / LCA / MATRIX / DP(bitmask) / 2 POINTERS / SEG TREE / MATH / UN FIND / MO
    Read the statement CAREFULLY !!
    Make a GREADY APPROACH !!!! (start from highest / lowest)
    Make your own TESTS !!
    Be careful from CORNER CASES !
*/

Compilation message

dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:96:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |             for(int j = 1 ; j < comp.size() ; j++) if(dp[comp[j]] < dp[comp[mn]]) mn = j;
      |                             ~~^~~~~~~~~~~~~
dreaming.cpp:104:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  104 |     for(int i = 0 ; i < v.size() - 1 ; i++){
      |                     ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 120 ms 28980 KB Output is correct
2 Correct 117 ms 28616 KB Output is correct
3 Correct 72 ms 23944 KB Output is correct
4 Correct 15 ms 10928 KB Output is correct
5 Correct 15 ms 9764 KB Output is correct
6 Correct 29 ms 12612 KB Output is correct
7 Correct 5 ms 7884 KB Output is correct
8 Correct 58 ms 16760 KB Output is correct
9 Correct 80 ms 20676 KB Output is correct
10 Correct 5 ms 8012 KB Output is correct
11 Correct 130 ms 23548 KB Output is correct
12 Correct 158 ms 26136 KB Output is correct
13 Correct 6 ms 8012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 7864 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 120 ms 28980 KB Output is correct
2 Correct 117 ms 28616 KB Output is correct
3 Correct 72 ms 23944 KB Output is correct
4 Correct 15 ms 10928 KB Output is correct
5 Correct 15 ms 9764 KB Output is correct
6 Correct 29 ms 12612 KB Output is correct
7 Correct 5 ms 7884 KB Output is correct
8 Correct 58 ms 16760 KB Output is correct
9 Correct 80 ms 20676 KB Output is correct
10 Correct 5 ms 8012 KB Output is correct
11 Correct 130 ms 23548 KB Output is correct
12 Correct 158 ms 26136 KB Output is correct
13 Correct 6 ms 8012 KB Output is correct
14 Incorrect 5 ms 7864 KB Output isn't correct
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 69 ms 17312 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 7864 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 120 ms 28980 KB Output is correct
2 Correct 117 ms 28616 KB Output is correct
3 Correct 72 ms 23944 KB Output is correct
4 Correct 15 ms 10928 KB Output is correct
5 Correct 15 ms 9764 KB Output is correct
6 Correct 29 ms 12612 KB Output is correct
7 Correct 5 ms 7884 KB Output is correct
8 Correct 58 ms 16760 KB Output is correct
9 Correct 80 ms 20676 KB Output is correct
10 Correct 5 ms 8012 KB Output is correct
11 Correct 130 ms 23548 KB Output is correct
12 Correct 158 ms 26136 KB Output is correct
13 Correct 6 ms 8012 KB Output is correct
14 Incorrect 5 ms 7864 KB Output isn't correct
15 Halted 0 ms 0 KB -