답안 #807052

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
807052 2023-08-04T12:52:12 Z YassirSalama 꿈 (IOI13_dreaming) C++17
47 / 100
757 ms 17652 KB
#include "dreaming.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) v.begin(),v.end()
#define OVL(x,s) for(auto y:x) cout<<y<<s; cout<<"\n";
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
#define pb push_back
#define F first
#define S second
const ll INF=1e9;
const int MAXN=1e5+100;
int n,m,l;
vector<vector<pair<int,int>>> v(MAXN);
vector<pair<ll,ll>> diameters;
bool visited[MAXN];
ll parr[MAXN];
ll depth[MAXN];
vector<int> abc;
pair<ll,ll> dfs(int node,int par,ll ew=0){//depth, best node
    visited[node]=true;
    parr[node]=par;
    depth[node]=ew;
    // dbg(node,par,ew);
    if(v[node].size()==1&&v[node][0].F==par){
        return {ew,node};
    }
    ll ans=0;
    ll best=node;
    for(auto x:v[node]){
        if(x.F!=par){
            // dbg(x.F,node,ew,x.S);
            pair<ll,ll> p=dfs(x.F,node,ew+x.S);
            if(p.F>ans){
                ans=p.F;
                best=p.S;
            }
        }
    }
    return {ans,best};
}
ll bigdia=0;
int par[MAXN];
int f=0;
void find_diameter(int node){
    pair<ll,ll> p=dfs(node,node);
    int f=p.S;
    memset(parr,0,sizeof(parr));
    p=dfs(p.S,p.S);
    // dbg(f,p.S);
    vector<ll> dia;
    while(p.S!=parr[p.S]) {
        dia.push_back(p.S);
        p.S=parr[p.S];
    }
    dia.push_back(p.S);
    reverse(all(dia));
    bigdia=p.F;
    abc.push_back(p.F);
    int nodea=0;
    int nodeb=0;
    int a,b,c,e;
    ll besta=INF;
    ll bestb=INF;
    for(auto x:dia){
        a=depth[dia.back()]-depth[x];
        b=depth[x];
        if(besta>max(a,b)){
            besta=max(a,b);
            nodea=x;
        }
        par[x]=dia[0];
    }
    // if(besta==INF){
        // dbg(node);
    // }
    // assert(besta==INF);
    diameters.pb({besta,nodea});
    // dbg(besta,nodea);
    // OVL(dia," ")
    f++;
}
struct Edge{
    ll a,b;
    ll c;
    bool operator < (const Edge& other) const{
        return c<other.c;
    }
};
int find(int node){
    if(node==par[node]) return node;
    return par[node]=find(par[node]);
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
    n=N;
    m=M;
    l=L;
    memset(visited,false,sizeof(visited));
    for(int i=0;i<m;i++){
        v[A[i]].push_back({B[i],T[i]});
        v[B[i]].push_back({A[i],T[i]});
    }
    memset(depth,0,sizeof(depth));
    for(int i=0;i<n;i++){
        if(!visited[i]){
            find_diameter(i);
        }
    }
    vector<Edge> edges;
    int d=diameters.size();
    // cout<<d<<endl;
    bool ok=true;
    for(int i=0;i<n;i++){
        ok&=(v[i].size()<=1);
    }
    sort(all(abc));
    bigdia=abc.back();
    sort(all(diameters),greater<pair<int,int>>());
    if(diameters.size()==1) return bigdia;
    if(diameters.size()==2) return max(bigdia,diameters[0].F+diameters[1].F+l);
    else return max({bigdia
        ,diameters[diameters.size()-1].F+diameters[diameters.size()-2].F+l,
        diameters[diameters.size()-2].F+diameters[diameters.size()-3].F+2*L});
    for(int i=0;i<d-1;i++){
        int nodea=diameters[i].S;
        ll besta=diameters[i].F;
        int nodeb=-1;
        ll bestb=INF;
        edges.push_back({.a=nodea,.b=diameters[i+1].S,.c=besta+(i+1)*l+diameters[i+1].F});
        // dbg(nodea,nodeb);
    }
    sort(all(edges));
    for(auto& edge:edges){
        if(find(edge.a)==find(edge.b)) continue;
        int a=find(edge.a);
        int b=find(edge.b);
        par[b]=a;
        // dbg(edge.a,edge.b,edge.c);
        // dbg(a,b,edge.a,edge.b);
        v[edge.a].pb({edge.b,l});
        v[edge.b].pb({edge.a,l});
    }
    memset(depth,0LL,sizeof(depth));
    find_diameter(0);
    //working for sub4
    //here we have every element have at most one
    return bigdia;
}

#ifdef IOI

#define fail(s, x...) do { \
        fprintf(stderr, s "\n", ## x); \
        exit(1); \
    } while(0)

#define MAX_N 100000

static int A[MAX_N];
static int B[MAX_N];
static int T[MAX_N];

int main() {
    int N, M, L, i;
    int res;

    FILE *f = fopen("dreaming.in", "r");
    if (!f)
        fail("Failed to open input file.");

    res = fscanf(f, "%d%d%d", &N, &M, &L);
    for (i = 0; i < M; i++)
        res = fscanf(f, "%d%d%d", &A[i], &B[i], &T[i]);
    fclose(f);

    int answer = travelTime(N, M, L, A, B, T);
    printf("%d\n", answer);

    return 0;
}

#endif

Compilation message

dreaming.cpp: In function 'void find_diameter(int)':
dreaming.cpp:63:9: warning: unused variable 'nodeb' [-Wunused-variable]
   63 |     int nodeb=0;
      |         ^~~~~
dreaming.cpp:64:13: warning: unused variable 'c' [-Wunused-variable]
   64 |     int a,b,c,e;
      |             ^
dreaming.cpp:64:15: warning: unused variable 'e' [-Wunused-variable]
   64 |     int a,b,c,e;
      |               ^
dreaming.cpp:66:8: warning: unused variable 'bestb' [-Wunused-variable]
   66 |     ll bestb=INF;
      |        ^~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:129:13: warning: unused variable 'nodeb' [-Wunused-variable]
  129 |         int nodeb=-1;
      |             ^~~~~
dreaming.cpp:130:12: warning: unused variable 'bestb' [-Wunused-variable]
  130 |         ll bestb=INF;
      |            ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 16584 KB Output is correct
2 Correct 34 ms 16476 KB Output is correct
3 Correct 23 ms 12356 KB Output is correct
4 Correct 7 ms 6228 KB Output is correct
5 Correct 5 ms 5204 KB Output is correct
6 Correct 9 ms 7108 KB Output is correct
7 Correct 2 ms 4328 KB Output is correct
8 Correct 24 ms 8392 KB Output is correct
9 Correct 25 ms 10180 KB Output is correct
10 Correct 2 ms 4376 KB Output is correct
11 Correct 38 ms 11896 KB Output is correct
12 Correct 70 ms 14224 KB Output is correct
13 Correct 2 ms 4436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4308 KB Output is correct
2 Correct 3 ms 4324 KB Output is correct
3 Correct 2 ms 4308 KB Output is correct
4 Correct 2 ms 4308 KB Output is correct
5 Correct 2 ms 4308 KB Output is correct
6 Correct 2 ms 4308 KB Output is correct
7 Correct 3 ms 4308 KB Output is correct
8 Correct 2 ms 4308 KB Output is correct
9 Correct 2 ms 4308 KB Output is correct
10 Correct 2 ms 4308 KB Output is correct
11 Correct 2 ms 4308 KB Output is correct
12 Correct 3 ms 4308 KB Output is correct
13 Correct 3 ms 4308 KB Output is correct
14 Correct 2 ms 4308 KB Output is correct
15 Correct 2 ms 4352 KB Output is correct
16 Correct 2 ms 4308 KB Output is correct
17 Correct 2 ms 4308 KB Output is correct
18 Correct 2 ms 4308 KB Output is correct
19 Correct 2 ms 4308 KB Output is correct
20 Correct 2 ms 4308 KB Output is correct
21 Correct 2 ms 4308 KB Output is correct
22 Correct 2 ms 4308 KB Output is correct
23 Correct 2 ms 4308 KB Output is correct
24 Correct 2 ms 4308 KB Output is correct
25 Correct 2 ms 4308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 16584 KB Output is correct
2 Correct 34 ms 16476 KB Output is correct
3 Correct 23 ms 12356 KB Output is correct
4 Correct 7 ms 6228 KB Output is correct
5 Correct 5 ms 5204 KB Output is correct
6 Correct 9 ms 7108 KB Output is correct
7 Correct 2 ms 4328 KB Output is correct
8 Correct 24 ms 8392 KB Output is correct
9 Correct 25 ms 10180 KB Output is correct
10 Correct 2 ms 4376 KB Output is correct
11 Correct 38 ms 11896 KB Output is correct
12 Correct 70 ms 14224 KB Output is correct
13 Correct 2 ms 4436 KB Output is correct
14 Correct 2 ms 4308 KB Output is correct
15 Correct 3 ms 4324 KB Output is correct
16 Correct 2 ms 4308 KB Output is correct
17 Correct 2 ms 4308 KB Output is correct
18 Correct 2 ms 4308 KB Output is correct
19 Correct 2 ms 4308 KB Output is correct
20 Correct 3 ms 4308 KB Output is correct
21 Correct 2 ms 4308 KB Output is correct
22 Correct 2 ms 4308 KB Output is correct
23 Correct 2 ms 4308 KB Output is correct
24 Correct 2 ms 4308 KB Output is correct
25 Correct 3 ms 4308 KB Output is correct
26 Correct 3 ms 4308 KB Output is correct
27 Correct 2 ms 4308 KB Output is correct
28 Correct 2 ms 4352 KB Output is correct
29 Correct 2 ms 4308 KB Output is correct
30 Correct 2 ms 4308 KB Output is correct
31 Correct 2 ms 4308 KB Output is correct
32 Correct 2 ms 4308 KB Output is correct
33 Correct 2 ms 4308 KB Output is correct
34 Correct 2 ms 4308 KB Output is correct
35 Correct 2 ms 4308 KB Output is correct
36 Correct 2 ms 4308 KB Output is correct
37 Correct 2 ms 4308 KB Output is correct
38 Correct 2 ms 4308 KB Output is correct
39 Correct 33 ms 16476 KB Output is correct
40 Correct 42 ms 16512 KB Output is correct
41 Correct 23 ms 12376 KB Output is correct
42 Correct 6 ms 6228 KB Output is correct
43 Correct 2 ms 4308 KB Output is correct
44 Correct 2 ms 4308 KB Output is correct
45 Correct 2 ms 4308 KB Output is correct
46 Correct 2 ms 4308 KB Output is correct
47 Correct 2 ms 4308 KB Output is correct
48 Correct 2 ms 4308 KB Output is correct
49 Correct 2 ms 4308 KB Output is correct
50 Correct 2 ms 4308 KB Output is correct
51 Correct 2 ms 4308 KB Output is correct
52 Correct 2 ms 4308 KB Output is correct
53 Correct 2 ms 4308 KB Output is correct
54 Correct 2 ms 4268 KB Output is correct
55 Correct 2 ms 4308 KB Output is correct
56 Correct 2 ms 4308 KB Output is correct
57 Correct 49 ms 9792 KB Output is correct
58 Correct 52 ms 9796 KB Output is correct
59 Correct 38 ms 9744 KB Output is correct
60 Correct 54 ms 9988 KB Output is correct
61 Correct 38 ms 9712 KB Output is correct
62 Correct 43 ms 9600 KB Output is correct
63 Correct 35 ms 9200 KB Output is correct
64 Correct 41 ms 9192 KB Output is correct
65 Correct 38 ms 9304 KB Output is correct
66 Correct 38 ms 9288 KB Output is correct
67 Correct 39 ms 9720 KB Output is correct
68 Correct 40 ms 9832 KB Output is correct
69 Correct 47 ms 9800 KB Output is correct
70 Correct 45 ms 9732 KB Output is correct
71 Correct 2 ms 4308 KB Output is correct
72 Correct 3 ms 4436 KB Output is correct
73 Correct 3 ms 4436 KB Output is correct
74 Correct 3 ms 4488 KB Output is correct
75 Correct 3 ms 4436 KB Output is correct
76 Correct 3 ms 4436 KB Output is correct
77 Correct 3 ms 4436 KB Output is correct
78 Correct 3 ms 4436 KB Output is correct
79 Correct 3 ms 4436 KB Output is correct
80 Correct 39 ms 9804 KB Output is correct
81 Correct 42 ms 9800 KB Output is correct
82 Correct 60 ms 9624 KB Output is correct
83 Correct 39 ms 9676 KB Output is correct
84 Correct 3 ms 4436 KB Output is correct
85 Correct 2 ms 4436 KB Output is correct
86 Correct 2 ms 4436 KB Output is correct
87 Correct 3 ms 4464 KB Output is correct
88 Correct 2 ms 4436 KB Output is correct
89 Correct 2 ms 4436 KB Output is correct
90 Correct 3 ms 4436 KB Output is correct
91 Correct 3 ms 4436 KB Output is correct
92 Correct 3 ms 4436 KB Output is correct
93 Correct 3 ms 4436 KB Output is correct
94 Correct 2 ms 4308 KB Output is correct
95 Correct 3 ms 4308 KB Output is correct
96 Correct 2 ms 4308 KB Output is correct
97 Correct 2 ms 4308 KB Output is correct
98 Correct 2 ms 4308 KB Output is correct
99 Correct 3 ms 4308 KB Output is correct
100 Correct 3 ms 4308 KB Output is correct
101 Correct 2 ms 4280 KB Output is correct
102 Correct 2 ms 4308 KB Output is correct
103 Correct 2 ms 4308 KB Output is correct
104 Correct 5 ms 5204 KB Output is correct
105 Correct 12 ms 7096 KB Output is correct
106 Correct 2 ms 4268 KB Output is correct
107 Correct 18 ms 8364 KB Output is correct
108 Correct 22 ms 10196 KB Output is correct
109 Correct 2 ms 4308 KB Output is correct
110 Correct 32 ms 11712 KB Output is correct
111 Correct 38 ms 14312 KB Output is correct
112 Correct 2 ms 4436 KB Output is correct
113 Correct 47 ms 17652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 757 ms 8412 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4308 KB Output is correct
2 Correct 3 ms 4324 KB Output is correct
3 Correct 2 ms 4308 KB Output is correct
4 Correct 2 ms 4308 KB Output is correct
5 Correct 2 ms 4308 KB Output is correct
6 Correct 2 ms 4308 KB Output is correct
7 Correct 3 ms 4308 KB Output is correct
8 Correct 2 ms 4308 KB Output is correct
9 Correct 2 ms 4308 KB Output is correct
10 Correct 2 ms 4308 KB Output is correct
11 Correct 2 ms 4308 KB Output is correct
12 Correct 3 ms 4308 KB Output is correct
13 Correct 3 ms 4308 KB Output is correct
14 Correct 2 ms 4308 KB Output is correct
15 Correct 2 ms 4352 KB Output is correct
16 Correct 2 ms 4308 KB Output is correct
17 Correct 2 ms 4308 KB Output is correct
18 Correct 2 ms 4308 KB Output is correct
19 Correct 2 ms 4308 KB Output is correct
20 Correct 2 ms 4308 KB Output is correct
21 Correct 2 ms 4308 KB Output is correct
22 Correct 2 ms 4308 KB Output is correct
23 Correct 2 ms 4308 KB Output is correct
24 Correct 2 ms 4308 KB Output is correct
25 Correct 2 ms 4308 KB Output is correct
26 Correct 4 ms 4308 KB Output is correct
27 Correct 8 ms 4352 KB Output is correct
28 Correct 11 ms 4524 KB Output is correct
29 Incorrect 5 ms 4404 KB Output isn't correct
30 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 16584 KB Output is correct
2 Correct 34 ms 16476 KB Output is correct
3 Correct 23 ms 12356 KB Output is correct
4 Correct 7 ms 6228 KB Output is correct
5 Correct 5 ms 5204 KB Output is correct
6 Correct 9 ms 7108 KB Output is correct
7 Correct 2 ms 4328 KB Output is correct
8 Correct 24 ms 8392 KB Output is correct
9 Correct 25 ms 10180 KB Output is correct
10 Correct 2 ms 4376 KB Output is correct
11 Correct 38 ms 11896 KB Output is correct
12 Correct 70 ms 14224 KB Output is correct
13 Correct 2 ms 4436 KB Output is correct
14 Correct 2 ms 4308 KB Output is correct
15 Correct 3 ms 4324 KB Output is correct
16 Correct 2 ms 4308 KB Output is correct
17 Correct 2 ms 4308 KB Output is correct
18 Correct 2 ms 4308 KB Output is correct
19 Correct 2 ms 4308 KB Output is correct
20 Correct 3 ms 4308 KB Output is correct
21 Correct 2 ms 4308 KB Output is correct
22 Correct 2 ms 4308 KB Output is correct
23 Correct 2 ms 4308 KB Output is correct
24 Correct 2 ms 4308 KB Output is correct
25 Correct 3 ms 4308 KB Output is correct
26 Correct 3 ms 4308 KB Output is correct
27 Correct 2 ms 4308 KB Output is correct
28 Correct 2 ms 4352 KB Output is correct
29 Correct 2 ms 4308 KB Output is correct
30 Correct 2 ms 4308 KB Output is correct
31 Correct 2 ms 4308 KB Output is correct
32 Correct 2 ms 4308 KB Output is correct
33 Correct 2 ms 4308 KB Output is correct
34 Correct 2 ms 4308 KB Output is correct
35 Correct 2 ms 4308 KB Output is correct
36 Correct 2 ms 4308 KB Output is correct
37 Correct 2 ms 4308 KB Output is correct
38 Correct 2 ms 4308 KB Output is correct
39 Correct 33 ms 16476 KB Output is correct
40 Correct 42 ms 16512 KB Output is correct
41 Correct 23 ms 12376 KB Output is correct
42 Correct 6 ms 6228 KB Output is correct
43 Correct 2 ms 4308 KB Output is correct
44 Correct 2 ms 4308 KB Output is correct
45 Correct 2 ms 4308 KB Output is correct
46 Correct 2 ms 4308 KB Output is correct
47 Correct 2 ms 4308 KB Output is correct
48 Correct 2 ms 4308 KB Output is correct
49 Correct 2 ms 4308 KB Output is correct
50 Correct 2 ms 4308 KB Output is correct
51 Correct 2 ms 4308 KB Output is correct
52 Correct 2 ms 4308 KB Output is correct
53 Correct 2 ms 4308 KB Output is correct
54 Correct 2 ms 4268 KB Output is correct
55 Correct 2 ms 4308 KB Output is correct
56 Correct 2 ms 4308 KB Output is correct
57 Correct 49 ms 9792 KB Output is correct
58 Correct 52 ms 9796 KB Output is correct
59 Correct 38 ms 9744 KB Output is correct
60 Correct 54 ms 9988 KB Output is correct
61 Correct 38 ms 9712 KB Output is correct
62 Correct 43 ms 9600 KB Output is correct
63 Correct 35 ms 9200 KB Output is correct
64 Correct 41 ms 9192 KB Output is correct
65 Correct 38 ms 9304 KB Output is correct
66 Correct 38 ms 9288 KB Output is correct
67 Correct 39 ms 9720 KB Output is correct
68 Correct 40 ms 9832 KB Output is correct
69 Correct 47 ms 9800 KB Output is correct
70 Correct 45 ms 9732 KB Output is correct
71 Correct 2 ms 4308 KB Output is correct
72 Correct 3 ms 4436 KB Output is correct
73 Correct 3 ms 4436 KB Output is correct
74 Correct 3 ms 4488 KB Output is correct
75 Correct 3 ms 4436 KB Output is correct
76 Correct 3 ms 4436 KB Output is correct
77 Correct 3 ms 4436 KB Output is correct
78 Correct 3 ms 4436 KB Output is correct
79 Correct 3 ms 4436 KB Output is correct
80 Correct 39 ms 9804 KB Output is correct
81 Correct 42 ms 9800 KB Output is correct
82 Correct 60 ms 9624 KB Output is correct
83 Correct 39 ms 9676 KB Output is correct
84 Correct 3 ms 4436 KB Output is correct
85 Correct 2 ms 4436 KB Output is correct
86 Correct 2 ms 4436 KB Output is correct
87 Correct 3 ms 4464 KB Output is correct
88 Correct 2 ms 4436 KB Output is correct
89 Correct 2 ms 4436 KB Output is correct
90 Correct 3 ms 4436 KB Output is correct
91 Correct 3 ms 4436 KB Output is correct
92 Correct 3 ms 4436 KB Output is correct
93 Correct 3 ms 4436 KB Output is correct
94 Correct 2 ms 4308 KB Output is correct
95 Correct 3 ms 4308 KB Output is correct
96 Correct 2 ms 4308 KB Output is correct
97 Correct 2 ms 4308 KB Output is correct
98 Correct 2 ms 4308 KB Output is correct
99 Correct 3 ms 4308 KB Output is correct
100 Correct 3 ms 4308 KB Output is correct
101 Correct 2 ms 4280 KB Output is correct
102 Correct 2 ms 4308 KB Output is correct
103 Correct 2 ms 4308 KB Output is correct
104 Correct 5 ms 5204 KB Output is correct
105 Correct 12 ms 7096 KB Output is correct
106 Correct 2 ms 4268 KB Output is correct
107 Correct 18 ms 8364 KB Output is correct
108 Correct 22 ms 10196 KB Output is correct
109 Correct 2 ms 4308 KB Output is correct
110 Correct 32 ms 11712 KB Output is correct
111 Correct 38 ms 14312 KB Output is correct
112 Correct 2 ms 4436 KB Output is correct
113 Correct 47 ms 17652 KB Output is correct
114 Incorrect 757 ms 8412 KB Output isn't correct
115 Halted 0 ms 0 KB -