답안 #82820

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
82820 2018-11-01T20:46:20 Z farukkastamonuda Inspection (POI11_ins) C++14
90 / 100
2599 ms 132096 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
 
const int MAXN = 1000123;

const int NOANS = -1;
 
 
int deg[MAXN];

vector<int> v[MAXN];

int n;

bool used[MAXN];

int myDeg[MAXN], father[MAXN], sizeOfSubtree[MAXN], depth[MAXN], maxSubtree[MAXN];

LL cost[MAXN];

queue<int> q;

queue<int> q2;

vector<int> cands;

vector<int> getCandidates;

void dfs(int x) {
    used[x] = true;
    for(int  i = 0; i < (int)v[x].size(); i ++) if (!used[v[x][i]]) {
		
        father[v[x][i]] = x;
        
        dfs(v[x][i]);
    }
}
 
vector<int> getCands(int root) {
    
    for(int i = 0; i < n; i ++) {
        myDeg[i] = deg[i];
        
        used[i] = false;
        
        father[i] = - 1;
        
        
        sizeOfSubtree[i] = 1;
        
        maxSubtree[i] = 0;
        
    }
    
    dfs(root);
    
 
    
    for(int i = 0; i < n; i ++) if (myDeg[i] == 1) {
		
        q.push(i);
    }
    while (! q.empty()) {
		
        int wz1 = q.front(); q.pop();
        
        myDeg[father[wz1]] --;
        
        if (std::max(n - sizeOfSubtree[wz1], maxSubtree[wz1]) <= n / 2) {
			
            cands.push_back(wz1);
            
        }
        
        sizeOfSubtree[father[wz1]] += sizeOfSubtree[wz1];
        
        maxSubtree[father[wz1]] = std::max(maxSubtree[father[wz1]], sizeOfSubtree[wz1]);
        
 
        if (myDeg[father[wz1]] == 1 && father[wz1] != - 1) {
			
            q.push(father[wz1]);
            
        }
    }
    return cands;
}
 
LL anal(int root) {
	
    for(int i = 0; i < n; i ++) {
		
        myDeg[i] = deg[i];
        
        used[i] = false;
        
        father[i] = - 1;
        
        sizeOfSubtree[i] = 1;
        
        cost[i] = 0;
        
        depth[i] = 0;
        
    }
    
    dfs(root);
 
    
    for(int i = 0; i < n; i++) if (myDeg[i] == 1) {
		
        q2.push(i);
        
    }
 
    while (! q2.empty()) {
        int wz1 = q2.front(); 
        
        q2.pop();
        
        myDeg[father[wz1]] --;
        
        sizeOfSubtree[father[wz1]] += sizeOfSubtree[wz1];
        
        cost[father[wz1]] += (2L * sizeOfSubtree[wz1] + cost[wz1]);
        
        depth[father[wz1]] = std::max(depth[father[wz1]], depth[wz1] + 1);
        
 
        if (myDeg[father[wz1]] == 1 && father[wz1] != - 1) {
			
            q2.push(father[wz1]);
            
        }
    }
 
    int maxDepth = 0;
    
    for(int i = 0; i < (int)v[root].size(); i ++) {
		
        maxDepth = std::max(maxDepth, depth[v[root][i]]);
        
    }
    for(int i = 0; i < (int)v[root].size(); i ++) {
		
        if (sizeOfSubtree[v[root][i]] ==  n / 2) {
			
            maxDepth = depth[v[root][i]];
            
        }
    }
    return cost[root] - (maxDepth + 1);
}
 
int main() {
	
    int a, b;
    
    scanf("%d", &n);
    
    if (n == 1) {
		
        printf("0\n");
        
        return 0;
        
    }
    for(int i = 0; i < n - 1; i ++) {
		
        scanf("%d %d", &a, &b);
        
        a --; b --;
        
        v[a].push_back(b);
        
        v[b].push_back(a);
        
        deg[a] ++;
        
        deg[b] ++;
        
    }
 
   getCandidates = getCands(n - 1);
   
    if (getCandidates.size() == 1) {
		
        int a = getCandidates[0]; 
        
        for(int i = 0; i < a; i++) {
			
            printf("%d\n", NOANS);
            
        }
        printf("%lld\n", anal(a));
        
        for(int i = a + 1; i < n; i++) {
			
            printf("%d\n", NOANS);
            
        }
        
    } else if (getCandidates.size() == 2) {
		
        int a = getCandidates[ 0 ], b = getCandidates[ 1 ];
        if (a > b) swap(a, b);
        for(int i = 0; i < a; i ++) {
            printf("%d\n", NOANS);
        }
        printf("%lld\n", anal(a));
        
        for(int i = a + 1; i < b; i ++) {
            printf("%d\n", NOANS);
        }
        
        
        printf("%lld\n", anal(b));
        
        for(int i = b + 1; i < n; i ++) {
			
            printf("%d\n", NOANS);
            
        }
        
    } else {
		
        assert(false);
        
    }
    
    return 0;
}

Compilation message

ins.cpp: In function 'int main()':
ins.cpp:162:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
ins.cpp:173:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &a, &b);
         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23928 KB Output is correct
2 Correct 30 ms 24036 KB Output is correct
3 Correct 24 ms 24128 KB Output is correct
4 Correct 23 ms 24128 KB Output is correct
5 Correct 22 ms 24128 KB Output is correct
6 Correct 23 ms 24128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 24172 KB Output is correct
2 Correct 23 ms 24172 KB Output is correct
3 Correct 24 ms 24176 KB Output is correct
4 Correct 23 ms 24180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 24328 KB Output is correct
2 Correct 25 ms 24348 KB Output is correct
3 Correct 23 ms 24384 KB Output is correct
4 Correct 25 ms 24420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 25864 KB Output is correct
2 Correct 39 ms 26308 KB Output is correct
3 Correct 48 ms 26820 KB Output is correct
4 Correct 38 ms 26820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 63 ms 27952 KB Output is correct
2 Correct 56 ms 28424 KB Output is correct
3 Correct 57 ms 28768 KB Output is correct
4 Correct 49 ms 28768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 121 ms 32184 KB Output is correct
2 Correct 130 ms 33320 KB Output is correct
3 Correct 125 ms 34416 KB Output is correct
4 Correct 103 ms 34416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 936 ms 60916 KB Output is correct
2 Correct 859 ms 66572 KB Output is correct
3 Correct 843 ms 72228 KB Output is correct
4 Correct 702 ms 72228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2599 ms 96080 KB Output is correct
2 Correct 2284 ms 106620 KB Output is correct
3 Correct 2298 ms 118024 KB Output is correct
4 Correct 1862 ms 118024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2545 ms 118024 KB Output is correct
2 Correct 2351 ms 118024 KB Output is correct
3 Correct 2306 ms 118544 KB Output is correct
4 Correct 1873 ms 118544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2473 ms 118544 KB Output is correct
2 Correct 2319 ms 121036 KB Output is correct
3 Runtime error 2199 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
4 Halted 0 ms 0 KB -