제출 #542824

#제출 시각아이디문제언어결과실행 시간메모리
542824zggf고대 책들 (IOI17_books)C++14
0 / 100
1 ms308 KiB
#include "books.h" #include <bits/stdc++.h> using namespace std; vector<int> parent, ranga; int find(int x){ if(parent[x]==-1) return x; parent[x] = find(parent[x]); return parent[x]; } void join(int a, int b){ a = find(a); b = find(b); if(a==b) return; if(ranga[a]>ranga[b]){ parent[b] = a; }else if(ranga[b]>ranga[a]){ parent[a] = b; }else{ parent[b] = a; ranga[a]++; } } long long minimum_walk(std::vector<int> p, int s) { int n = p.size(); vector<int> odw(n); parent.resize(n, -1); ranga.resize(n); long long wyn = 0; queue<int> q; vector<int> cost(n, 1e9); for(int i = 0; i < n; i++){ int pt = i; if(!odw[pt]){ if(p[pt]!=pt){ while(!odw[pt]){ odw[pt] = true; cost[pt] = 0; q.push(pt); wyn+=abs(p[pt]-pt); join(pt, p[pt]); pt = p[pt]; } }else{ odw[pt] = true; if(pt==s) cost[s] = 0, q.push(s); } } } while(q.size()){ int x = q.front(); //cout<<x<<" "<<cost[x]<<'\n'; q.pop(); if(x>0) if(find(x-1)!=find(x)) if(cost[x-1]==1e9){ cost[x-1] = cost[x]+1; q.push(x-1); join(x-1, x); }else{ wyn+=2*(cost[x-1]+cost[x]+1); join(x-1, x); } if(x<n-1) if(find(x+1)!=find(x)) if(cost[x+1]==1e9){ cost[x+1] = cost[x]+1; q.push(x+1); join(x+1, x); }else{ wyn+=2*(cost[x+1]+cost[x]+1); join(x+1, x); } } return wyn; }

컴파일 시 표준 에러 (stderr) 메시지

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:59:13: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
   59 |   if(x>0) if(find(x-1)!=find(x))
      |             ^
books.cpp:68:15: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
   68 |   if(x<n-1) if(find(x+1)!=find(x))
      |               ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...