Submission #1027440

# Submission time Handle Problem Language Result Execution time Memory
1027440 2024-07-19T06:27:18 Z 김은성(#10950) Telegraph (JOI16_telegraph) C++17
10 / 100
1000 ms 4780 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 0x3ffffffffffffff;
bool ch[100009];
int a[100009], c[100009];
vector<int> route;
vector<int> graph[100009];
ll cyc[100009];
ll sum[100009], mx[100009], mx2[100009];
void findcycle(int v){
   // printf("v=%d\n", v);
    route.push_back(v);
    if(ch[v]){
        int i;
        for(i=0; i<route.size(); i++){
            if(route[i] == v)
                break;
        }
        for(; i+1<route.size(); i++){
            cyc[route[i+1]] = c[route[i]];
        }
        return;
    }
    ch[v] = 1;
    findcycle(a[v]);
}
void dfs(int v){
    if(ch[v])
        return;
    route.push_back(v);
    ch[v]=1;
    for(int i=0; i<graph[v].size(); i++)
        dfs(graph[v][i]);
}
int main(){
    int n, i, comp = 0;
    scanf("%d", &n);
    for(i=1; i<=n; i++){
        scanf("%d %d", &a[i], &c[i]);
        graph[i].push_back(a[i]);
        graph[a[i]].push_back(i);
        if(c[i] > mx[a[i]]){
            mx2[a[i]] = mx[a[i]];
            mx[a[i]] = c[i];
        }
        else if(c[i] > mx2[a[i]]){
            mx2[a[i]] = c[i];
        }
        sum[a[i]] += c[i];
    }
    vector<int> perm;
    ll tans = INF;
    for(i=1; i<=n; i++)
        perm.push_back(i);
    do{
        ll now = 0;
        for(i=0; i<n; i++){
            int x = perm[i], y = perm[(i+1)%n];
            if(a[x] != y)
                now += c[x];
        }
        tans = min(now, tans);
    } while(next_permutation(perm.begin(), perm.end()));
    printf("%lld\n", tans);
    return 0;
    for(i=1; i<=n; i++){
        route.clear();
        findcycle(i);
    }
    ll ans = 0;
    memset(ch, 0, sizeof(ch));
    for(i=1; i<=n; i++){
        if(!ch[i]){
            comp++;
            route.clear();
            dfs(i);
            int j;
            ll temp = 0, mn = INF;
            bool flag=0;
            for(j=0; j<route.size(); j++){
                //printf("cyc=%d\n", cyc[route[j]]);
                if(cyc[route[j]])
                    mn = min(mn, cyc[route[j]]);
                if(!mx2[route[j]])
                    continue;
                temp += sum[route[j]] - mx[route[j]];
                if(cyc[route[j]] && mx[route[j]] && cyc[route[j]] != mx[route[j]])
                    flag = 1;
                mn = min(mn, mx[route[j]] - mx2[route[j]]);
            }
            //printf("i=%d temp=%lld mn=%lld\n", i, temp, mn);
            if(!flag)
                temp += mn;
            ans += temp;
        }
    }
    if(comp==1){
        for(i=1; i<=n; i++){
            if(!cyc[i])
                break;
        }
        if(i==n+1){
            ans=0;
        }
    }
    printf("%lld\n", ans);
    return 0;
}

Compilation message

telegraph.cpp: In function 'void findcycle(int)':
telegraph.cpp:16:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |         for(i=0; i<route.size(); i++){
      |                  ~^~~~~~~~~~~~~
telegraph.cpp:20:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |         for(; i+1<route.size(); i++){
      |               ~~~^~~~~~~~~~~~~
telegraph.cpp: In function 'void dfs(int)':
telegraph.cpp:33:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |     for(int i=0; i<graph[v].size(); i++)
      |                  ~^~~~~~~~~~~~~~~~
telegraph.cpp: In function 'int main()':
telegraph.cpp:81:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |             for(j=0; j<route.size(); j++){
      |                      ~^~~~~~~~~~~~~
telegraph.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
telegraph.cpp:40:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |         scanf("%d %d", &a[i], &c[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 86 ms 4536 KB Output is correct
2 Correct 84 ms 4444 KB Output is correct
3 Correct 88 ms 4692 KB Output is correct
4 Correct 85 ms 4444 KB Output is correct
5 Correct 84 ms 4444 KB Output is correct
6 Correct 89 ms 4440 KB Output is correct
7 Correct 88 ms 4440 KB Output is correct
8 Correct 89 ms 4444 KB Output is correct
9 Correct 87 ms 4440 KB Output is correct
10 Correct 88 ms 4444 KB Output is correct
11 Correct 84 ms 4444 KB Output is correct
12 Correct 89 ms 4688 KB Output is correct
13 Correct 85 ms 4444 KB Output is correct
14 Correct 86 ms 4532 KB Output is correct
15 Correct 87 ms 4444 KB Output is correct
16 Correct 87 ms 4536 KB Output is correct
17 Correct 86 ms 4440 KB Output is correct
18 Correct 85 ms 4444 KB Output is correct
19 Correct 87 ms 4532 KB Output is correct
20 Correct 78 ms 4440 KB Output is correct
21 Correct 89 ms 4552 KB Output is correct
22 Correct 90 ms 4440 KB Output is correct
23 Correct 88 ms 4440 KB Output is correct
24 Correct 86 ms 4444 KB Output is correct
25 Correct 83 ms 4444 KB Output is correct
26 Correct 85 ms 4440 KB Output is correct
27 Correct 87 ms 4440 KB Output is correct
28 Correct 87 ms 4532 KB Output is correct
29 Correct 91 ms 4444 KB Output is correct
30 Correct 82 ms 4536 KB Output is correct
31 Correct 93 ms 4444 KB Output is correct
32 Correct 85 ms 4536 KB Output is correct
33 Correct 91 ms 4440 KB Output is correct
34 Correct 91 ms 4532 KB Output is correct
35 Correct 89 ms 4444 KB Output is correct
36 Correct 87 ms 4444 KB Output is correct
37 Correct 75 ms 4536 KB Output is correct
38 Correct 73 ms 4440 KB Output is correct
39 Correct 87 ms 4780 KB Output is correct
40 Correct 86 ms 4536 KB Output is correct
41 Correct 1 ms 4444 KB Output is correct
42 Correct 1 ms 4440 KB Output is correct
43 Correct 1 ms 4548 KB Output is correct
44 Correct 1 ms 4444 KB Output is correct
45 Correct 1 ms 4444 KB Output is correct
46 Correct 1 ms 4444 KB Output is correct
47 Correct 89 ms 4532 KB Output is correct
48 Correct 91 ms 4536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 4536 KB Output is correct
2 Correct 84 ms 4444 KB Output is correct
3 Correct 88 ms 4692 KB Output is correct
4 Correct 85 ms 4444 KB Output is correct
5 Correct 84 ms 4444 KB Output is correct
6 Correct 89 ms 4440 KB Output is correct
7 Correct 88 ms 4440 KB Output is correct
8 Correct 89 ms 4444 KB Output is correct
9 Correct 87 ms 4440 KB Output is correct
10 Correct 88 ms 4444 KB Output is correct
11 Correct 84 ms 4444 KB Output is correct
12 Correct 89 ms 4688 KB Output is correct
13 Correct 85 ms 4444 KB Output is correct
14 Correct 86 ms 4532 KB Output is correct
15 Correct 87 ms 4444 KB Output is correct
16 Correct 87 ms 4536 KB Output is correct
17 Correct 86 ms 4440 KB Output is correct
18 Correct 85 ms 4444 KB Output is correct
19 Correct 87 ms 4532 KB Output is correct
20 Correct 78 ms 4440 KB Output is correct
21 Correct 89 ms 4552 KB Output is correct
22 Correct 90 ms 4440 KB Output is correct
23 Correct 88 ms 4440 KB Output is correct
24 Correct 86 ms 4444 KB Output is correct
25 Correct 83 ms 4444 KB Output is correct
26 Correct 85 ms 4440 KB Output is correct
27 Correct 87 ms 4440 KB Output is correct
28 Correct 87 ms 4532 KB Output is correct
29 Correct 91 ms 4444 KB Output is correct
30 Correct 82 ms 4536 KB Output is correct
31 Correct 93 ms 4444 KB Output is correct
32 Correct 85 ms 4536 KB Output is correct
33 Correct 91 ms 4440 KB Output is correct
34 Correct 91 ms 4532 KB Output is correct
35 Correct 89 ms 4444 KB Output is correct
36 Correct 87 ms 4444 KB Output is correct
37 Correct 75 ms 4536 KB Output is correct
38 Correct 73 ms 4440 KB Output is correct
39 Correct 87 ms 4780 KB Output is correct
40 Correct 86 ms 4536 KB Output is correct
41 Correct 1 ms 4444 KB Output is correct
42 Correct 1 ms 4440 KB Output is correct
43 Correct 1 ms 4548 KB Output is correct
44 Correct 1 ms 4444 KB Output is correct
45 Correct 1 ms 4444 KB Output is correct
46 Correct 1 ms 4444 KB Output is correct
47 Correct 89 ms 4532 KB Output is correct
48 Correct 91 ms 4536 KB Output is correct
49 Execution timed out 1044 ms 4444 KB Time limit exceeded
50 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 86 ms 4536 KB Output is correct
2 Correct 84 ms 4444 KB Output is correct
3 Correct 88 ms 4692 KB Output is correct
4 Correct 85 ms 4444 KB Output is correct
5 Correct 84 ms 4444 KB Output is correct
6 Correct 89 ms 4440 KB Output is correct
7 Correct 88 ms 4440 KB Output is correct
8 Correct 89 ms 4444 KB Output is correct
9 Correct 87 ms 4440 KB Output is correct
10 Correct 88 ms 4444 KB Output is correct
11 Correct 84 ms 4444 KB Output is correct
12 Correct 89 ms 4688 KB Output is correct
13 Correct 85 ms 4444 KB Output is correct
14 Correct 86 ms 4532 KB Output is correct
15 Correct 87 ms 4444 KB Output is correct
16 Correct 87 ms 4536 KB Output is correct
17 Correct 86 ms 4440 KB Output is correct
18 Correct 85 ms 4444 KB Output is correct
19 Correct 87 ms 4532 KB Output is correct
20 Correct 78 ms 4440 KB Output is correct
21 Correct 89 ms 4552 KB Output is correct
22 Correct 90 ms 4440 KB Output is correct
23 Correct 88 ms 4440 KB Output is correct
24 Correct 86 ms 4444 KB Output is correct
25 Correct 83 ms 4444 KB Output is correct
26 Correct 85 ms 4440 KB Output is correct
27 Correct 87 ms 4440 KB Output is correct
28 Correct 87 ms 4532 KB Output is correct
29 Correct 91 ms 4444 KB Output is correct
30 Correct 82 ms 4536 KB Output is correct
31 Correct 93 ms 4444 KB Output is correct
32 Correct 85 ms 4536 KB Output is correct
33 Correct 91 ms 4440 KB Output is correct
34 Correct 91 ms 4532 KB Output is correct
35 Correct 89 ms 4444 KB Output is correct
36 Correct 87 ms 4444 KB Output is correct
37 Correct 75 ms 4536 KB Output is correct
38 Correct 73 ms 4440 KB Output is correct
39 Correct 87 ms 4780 KB Output is correct
40 Correct 86 ms 4536 KB Output is correct
41 Correct 1 ms 4444 KB Output is correct
42 Correct 1 ms 4440 KB Output is correct
43 Correct 1 ms 4548 KB Output is correct
44 Correct 1 ms 4444 KB Output is correct
45 Correct 1 ms 4444 KB Output is correct
46 Correct 1 ms 4444 KB Output is correct
47 Correct 89 ms 4532 KB Output is correct
48 Correct 91 ms 4536 KB Output is correct
49 Execution timed out 1044 ms 4444 KB Time limit exceeded
50 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 86 ms 4536 KB Output is correct
2 Correct 84 ms 4444 KB Output is correct
3 Correct 88 ms 4692 KB Output is correct
4 Correct 85 ms 4444 KB Output is correct
5 Correct 84 ms 4444 KB Output is correct
6 Correct 89 ms 4440 KB Output is correct
7 Correct 88 ms 4440 KB Output is correct
8 Correct 89 ms 4444 KB Output is correct
9 Correct 87 ms 4440 KB Output is correct
10 Correct 88 ms 4444 KB Output is correct
11 Correct 84 ms 4444 KB Output is correct
12 Correct 89 ms 4688 KB Output is correct
13 Correct 85 ms 4444 KB Output is correct
14 Correct 86 ms 4532 KB Output is correct
15 Correct 87 ms 4444 KB Output is correct
16 Correct 87 ms 4536 KB Output is correct
17 Correct 86 ms 4440 KB Output is correct
18 Correct 85 ms 4444 KB Output is correct
19 Correct 87 ms 4532 KB Output is correct
20 Correct 78 ms 4440 KB Output is correct
21 Correct 89 ms 4552 KB Output is correct
22 Correct 90 ms 4440 KB Output is correct
23 Correct 88 ms 4440 KB Output is correct
24 Correct 86 ms 4444 KB Output is correct
25 Correct 83 ms 4444 KB Output is correct
26 Correct 85 ms 4440 KB Output is correct
27 Correct 87 ms 4440 KB Output is correct
28 Correct 87 ms 4532 KB Output is correct
29 Correct 91 ms 4444 KB Output is correct
30 Correct 82 ms 4536 KB Output is correct
31 Correct 93 ms 4444 KB Output is correct
32 Correct 85 ms 4536 KB Output is correct
33 Correct 91 ms 4440 KB Output is correct
34 Correct 91 ms 4532 KB Output is correct
35 Correct 89 ms 4444 KB Output is correct
36 Correct 87 ms 4444 KB Output is correct
37 Correct 75 ms 4536 KB Output is correct
38 Correct 73 ms 4440 KB Output is correct
39 Correct 87 ms 4780 KB Output is correct
40 Correct 86 ms 4536 KB Output is correct
41 Correct 1 ms 4444 KB Output is correct
42 Correct 1 ms 4440 KB Output is correct
43 Correct 1 ms 4548 KB Output is correct
44 Correct 1 ms 4444 KB Output is correct
45 Correct 1 ms 4444 KB Output is correct
46 Correct 1 ms 4444 KB Output is correct
47 Correct 89 ms 4532 KB Output is correct
48 Correct 91 ms 4536 KB Output is correct
49 Execution timed out 1044 ms 4444 KB Time limit exceeded
50 Halted 0 ms 0 KB -