제출 #578036

#제출 시각아이디문제언어결과실행 시간메모리
578036Sam_a17관광지 (IZhO14_shymbulak)C++14
0 / 100
1593 ms17492 KiB
#define _CRT_SECURE_NO_WARNINGS #include <bits/stdc++.h> //#include "temp.cpp" #include <cstdio> using namespace std; #pragma GCC optimize("Ofast") #ifndef ONLINE_JUDGE #define dbg(x) cerr << #x <<" "; print(x); cerr << endl; #else #define dbg(x) #endif #define sz(x) (int((x).size())) #define len(x) (int)x.length() #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define clr(x) (x).clear() #define uniq(x) x.resize(unique(all(x)) - x.begin()); #define pb push_back #define popf pop_front #define popb pop_back void print(long long t) {cerr << t;} void print(int t) {cerr << t;} void print(string t) {cerr << t;} void print(char t) {cerr << t;} void print(double t) {cerr << t;} void print(long double t) {cerr << t;} void print(unsigned long long t) {cerr << t;} template <class T> void print(set <T> v); template <class T> void print(vector <T> v); template <class T> void print(multiset <T> v); template <class T, class V> void print(map <T, V> v); template <class T, class V> void print(pair <T, V> p); template <class T, class V> void print(T v[],V n) {cerr << "["; for(int i = 0; i < n; i++) {print(v[i]); cerr << " "; } cerr << "]";} template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";} template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";} template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";} template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";} template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";} template <class T, class V> void print(unordered_map<T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";} #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; #define nl '\n' // for grid problems long long dx[8] = {-1,0,1,0,1,-1,1,-1}; long long dy[8] = {0,1,0,-1,1,1,-1,-1}; // lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6 void fastIO() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); } // file in/out void setIO(string str = "") { fastIO(); if (str != "" && str != "input") { freopen((str + ".in").c_str(), "r", stdin); freopen((str + ".out").c_str(), "w", stdout); } if(str == "input") { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } } // Indexed Set template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const long long N = 2e5 + 10; vector<long long> adj[N], cycle; long long n, p[N], maxDist, depth[N]; pair<long long, long long> answNode[N]; bool used[N]; long long answ; bool flag = false; void getCycle(long long node, long long parent) { used[node] = true; p[node] = parent; for(auto i: adj[node]) { if(i == parent) continue; if(used[i]) { // we get cycle long long curr = node; while(curr != i) { // dbg(curr) cycle.push_back(curr); curr = p[curr]; } cycle.push_back(i); flag = true; // dbg(cycle) return; } else { getCycle(i, node); } if(flag) { return; } } } pair<long long, long long> getFurthest(long long node) { memset(used, false, sizeof(used)); queue<pair<long long, long long>> q; q.push({node, 0}); used[node] = true; pair<long long, long long> answ = {node, 0}; while(!q.empty()) { auto u = q.front(); q.pop(); for(auto i: adj[u.first]) { if(used[i]) continue; used[i] = true; q.push({i, u.second + 1}); if(u.second + 1 > answ.second) { answ = {i, u.second + 1}; } } } return answ; } // dp pair<long long, long long> dfs(long long node, long long parent) { vector<pair<long long, long long>> answers; for(auto i: adj[node]) { if(used[i] || i == parent) continue; depth[i] = depth[node] + 1; answers.push_back(dfs(i, node)); } if(sz(answers) >= 2) { sort(all(answers)); long long size = sz(answers); if(answers[size - 1].first == answers[size - 2].first) { long long dist = 2 * (answers[size - 1].first - depth[node]); long long ansi = 0, all = 0; size--; // dbg(answers) while(size >= 0 && answers[size].first == answers.back().first) { ansi += all * answers[size].second; all += answers[size--].second; } answers[sz(answers) - 1].second = all; if(dist != maxDist) { answNode[node] = {answers.back()}; return {answers.back()}; } answ += ansi; } else { long long dist = answers[size - 1].first + answers[size - 2].first - 2 * depth[node]; if(dist != maxDist) { answNode[node] = {answers.back()}; return {answers.back()}; } long long l = answers.back().second, r = 0, it = size - 2; size -= 2; while(size >= 0 && answers[size].first == answers[it].first) { r += answers[size--].second; } answ += l * r; } } if(answers.empty()) { answNode[node] = {depth[node], 1}; return {depth[node], 1}; } else { answNode[node] = {answers.back()}; return {answers.back()}; } } void solve_() { cin >> n; for(int i = 1; i <= n; i++) { long long a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } // we get the path which contain cycle getCycle(1, 0); // we should calc the max distance maxDist = getFurthest(getFurthest(1).first).second; dbg(cycle) // get cycle used memset(used, false, sizeof(used)); for(auto i: cycle) { used[i] = true; } for(auto i: cycle) { dfs(i, 0); } dbg(answ) for(int i = 0; i < sz(cycle); i++) { for(int j = i + 1; j < sz(cycle); j++) { long long minDist = min((long long)(j - i), (long long)(i + sz(cycle) - j)); long long A = cycle[i], B = cycle[j]; if(answNode[A].first + answNode[B].first + minDist == maxDist) { if(sz(cycle) % 2 == 0 && minDist == sz(cycle) / 2) { answ += 2 * (answNode[A].second * answNode[B].second); } else { answ += (answNode[A].second * answNode[B].second); } } } } printf("%lld\n", answ); } int main() { setIO(); auto solve = [&](int test_case)-> void { while(test_case--) { solve_(); } }; int test_cases = 1; solve(test_cases); return 0; }

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

shymbulak.cpp: In function 'void setIO(std::string)':
shymbulak.cpp:65:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
shymbulak.cpp:66:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
shymbulak.cpp:70:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
shymbulak.cpp:71:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |     freopen("output.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...