답안 #152294

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
152294 2019-09-07T04:41:26 Z arnold518 Telegraph (JOI16_telegraph) C++14
0 / 100
4 ms 2936 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e5;

int N, A[MAXN+10], C[MAXN+10];
ll S, val, ans;

vector<int> adj[MAXN+10];
int out[MAXN+10];

bool vis[MAXN+10];

vector<vector<int>> cyc;
vector<int> ST;
void dfs(int now)
{
    vis[now]=1; ST.push_back(now);
    if(out[now]==-1) { ST.pop_back(); return; }
    if(vis[out[now]]==1) cyc.push_back(ST);
    if(vis[out[now]]==0) dfs(out[now]);
    vis[now]=2; ST.pop_back();
}

int main()
{
    int i, j;

    scanf("%d", &N);
    for(i=1; i<=N; i++)
    {
        scanf("%d%lld", &A[i], &C[i]);
        adj[A[i]].push_back(i);
        S+=C[i];
    }

    memset(vis, 0, sizeof(vis));
    for(i=1; !vis[i]; i=A[i]) vis[i]=1;
    bool flag=true;
    for(i=1; i<=N; i++) flag=flag&&vis[i];
    if(flag) return !printf("0");

    for(i=1; i<=N; i++)
    {
        sort(adj[i].begin(), adj[i].end(), [&](const int &p, const int &q) { return C[p]>C[q]; });
        if(adj[i].empty()) out[i]=-1;
        else out[i]=adj[i][0], val+=C[out[i]];
    }

    memset(vis, 0, sizeof(vis));
    for(i=1; i<=N; i++) if(vis[i]==0) dfs(i);
    if(cyc.empty()) return !printf("%lld", S-val);

    //for(auto &it : cyc) { for(auto &jt : it) printf("%d ", jt); printf("\n"); }
    for(auto &it : cyc)
    {
        ll now=-1e15;
        for(auto &jt : it)
        {
            if(adj[jt].size()==1) now=max(now, -(ll)C[out[jt]]);
            else now=max(now, -(ll)C[adj[jt][0]]+C[adj[jt][1]]);
        }
        val+=now;
    }
    printf("%lld", S-val);
}

Compilation message

telegraph.cpp: In function 'int main()':
telegraph.cpp:36:37: warning: format '%lld' expects argument of type 'long long int*', but argument 3 has type 'int*' [-Wformat=]
         scanf("%d%lld", &A[i], &C[i]);
                                ~~~~~^
telegraph.cpp:31:12: warning: unused variable 'j' [-Wunused-variable]
     int i, j;
            ^
telegraph.cpp:33:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &N);
     ~~~~~^~~~~~~~~~
telegraph.cpp:36:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%lld", &A[i], &C[i]);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2936 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2936 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2936 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2936 KB Output isn't correct
2 Halted 0 ms 0 KB -