답안 #341572

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
341572 2020-12-30T04:18:03 Z beksultan04 관광지 (IZhO14_shymbulak) C++14
100 / 100
507 ms 123016 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define OK puts("OK");
#define NO puts("NO");
#define YES puts("YES");
#define fr first
#define sc second
#define ret return
#define scanl(a) scanf("%lld",&a);
#define scanll(a,b) scanf("%lld %lld",&a, &b);
#define scanlll(a,b,c) scanf("%lld %lld %lld",&a,&b,&c);
#define scan1(a) scanf("%d",&a);
#define scan2(a,b) scanf("%d %d",&a, &b);
#define scan3(a,b,c) scanf("%d %d %d",&a,&b,&c);
#define all(s) s.begin(),s.end()
#define allr(s) s.rbegin(),s.rend()
#define pb push_back
#define sz(v) (int)v.size()
#define endi puts("");
#define eps 1e-12
const int N = 2e6+12,INF=1e9+7;
vector <pii> gg[N];
vector <int> g[N],cycle,v;
pii ver[N];
bool bosh[N],vis[N];
void dfs_cycle(int x,int p){
    vis[x] = 1;
    int i,j;
    v.pb(x);
    for (i=0;i<g[x].size();++i){
        int to = g[x][i];
        if (vis[to] == 1 && to != p){
            bool f=0;
            for (j=0;j<v.size();++j){
                if (v[j] == to)f=1;
                if (f){
                    cycle.pb(v[j]);
                }

            }
        }
        if (vis[to] == 0){
            dfs_cycle(to,x);
        }
    }
    v.pop_back();
}


int dlina[N],colichestvo[N];
pii ans;
void dfs_diametr(int x,int p){
    int i;
    vector <pii> tmp;
    for (i=0;i<g[x].size();++i){
        if (g[x][i] == p || bosh[g[x][i]])continue;
        dfs_diametr(g[x][i],x);
        tmp.pb({dlina[g[x][i]],colichestvo[g[x][i]]});
    }
    if (tmp.size() == 0){
        colichestvo[x] = 1;
        ret;
    }
    sort(allr(tmp));
    int mx = tmp[0].fr,cnt = tmp[0].sc;
    for (i=1;i<tmp.size();++i){
        if (tmp[i].fr == mx){
            cnt += tmp[i].sc;
        }

    }
    dlina[x] = mx+1;
    colichestvo[x] = cnt;
}

void calc(int x,int y,int m){
    int dist = min(abs(x-y),m-abs(x-y));
    dist += dlina[cycle[x]];
    dist += dlina[cycle[y]];
    if (ans.fr < dist){
        ans.fr = dist;
        ans.sc=0;
    }
    if (dist == ans.fr){
        if (abs(x-y) == (m-abs(x-y))){
            ans.fr += max(colichestvo[cycle[x]],1ll)*max(1ll,colichestvo[cycle[y]])*2ll;
        }
        else
            ans.fr += colichestvo[cycle[x]]*colichestvo[cycle[y]]*1ll;
    }
}

void posledniy_dfs(int x,int p){
    vector <pii> tmp;
    int i;
    for (i=0;i<g[x].size();++i){
        if (g[x][i] == p || bosh[g[x][i]])continue;
        posledniy_dfs(g[x][i],x);
        tmp.pb({dlina[g[x][i]],colichestvo[g[x][i]]});
    }
    if (tmp.size() <2)ret;
    sort(allr(tmp));
    if (tmp.empty())ret;
    vector <int> xixi;
    int mx = tmp[0].fr+1,cnt=tmp[0].sc,f=0;
    if (mx == tmp[1].fr+1){
        xixi.pb(cnt);
        int sum=0,bla=0;
        for (i=1;i<tmp.size();++i){
            if (tmp[i].fr+1+mx > ans.fr){
                ans.fr = tmp[i].fr+1+mx;
                f=2;
            }
            if (ans.fr == tmp[i].fr+1+mx){
                xixi.pb(tmp[i].sc);
                f = max(f,1ll);
            }
            else break;
        }
        for (i=0;i<xixi.size();++i){
            sum+=xixi[i];
            bla += xixi[i]*xixi[i];
        }
        sum *= sum;
        sum-=bla;
        sum/=2;
        if (f==2)ans.sc=sum;
        else if (f==1){
            ans.sc+=sum;
        }
    }
    else {
        int sum=0,bla=0;

        for (i=1;i<tmp.size();++i){
            if (tmp[i].fr+1+mx > ans.fr){
                ans.fr = tmp[i].fr+1+mx;
                f=2;
            }
            if (ans.fr == tmp[i].fr+1+mx){
                xixi.pb(tmp[i].sc);
                sum += cnt*tmp[i].sc;
                f = max(f,1ll);
            }
            else break;
        }
        if (f==2)ans.sc=sum;
        else if (f==1){
            ans.sc+=sum;
        }

    }
}

pii merg(pii x,pii y){
	if(x.fr>y.fr)
		return x;
	if(y.fr>x.fr)
		return y;
	return {x.fr,x.sc+y.sc};
}

main(){

    int n,i,j;
    cin>>n;
    for (i=1;i<=n;++i){
        int x,y;
        cin>>x>>y;
        g[x].pb(y);
        g[y].pb(x);
		gg[x].pb({y,i});
		gg[y].pb({x,i});
    }
    dfs_cycle(1,0);
    int m = cycle.size();
    if (m == n){
        cout <<n;
        ret 0;
    }
    for (i=0;i<m;++i)
        bosh[cycle[i]] = 1;
    for (i=0;i<m;++i){
        dfs_diametr(cycle[i],0);
//        cout <<cycle[i]<<"-"<<dlina[cycle[i]]<<" ";
//        cout <<colichestvo[cycle[i]]<<"\n";
    }

    int sz = m;
    vector <int>otvet;
    for (i=0;i<m;++i)
        otvet.pb(cycle[i]);

    for (i=0;i<m-1;++i)
        otvet.pb(cycle[i]);

    for (i=0;i<otvet.size();++i){
        int vershina = otvet[i];

    }







	vector<pii>gg;
	for (i=0;i<m;++i)
		gg.pb({dlina[cycle[i]],colichestvo[cycle[i]]});



    map <int,int> mp;
	int p=0;
	for(int i=0;i<sz-1;i++){
		while(p < sz and p+p <= sz+i+i)
			mp[gg[p].fr+p]+=gg[p].sc,p++;
		mp[gg[i].fr+i] -= gg[i].sc;
		if(!mp[gg[i].fr+i])
			mp.erase(gg[i].fr+i);
		pii tmp=*mp.rbegin();
		ans = merg(ans,{tmp.fr + gg[i].fr-i, gg[i].sc * tmp.sc});
	}

	p=sz-1;
	mp.clear();

	for(int i=sz-1;i>=0;i--){
		while(p+p>=sz+i+i)
			mp[gg[p].fr+sz-p]+=gg[p].sc,p--;
		if(mp.size()){
			pii tmp=*mp.rbegin();
			ans=merg(ans,{tmp.fr+gg[i].fr+i,gg[i].sc*tmp.sc});
		}
	}





    for (i=0;i<m;++i){
        posledniy_dfs(cycle[i],0);
    }
    cout <<ans.sc;

}
/*
16
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
6 10
10 11
10 12
5 13
2 14
2 15
15 16
7 1


10
1 2
2 3
3 4
4 7
3 5
3 8
5 6
8 9
9 10
3 1
*/

Compilation message

shymbulak.cpp: In function 'void dfs_cycle(long long int, long long int)':
shymbulak.cpp:32:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |     for (i=0;i<g[x].size();++i){
      |              ~^~~~~~~~~~~~
shymbulak.cpp:36:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |             for (j=0;j<v.size();++j){
      |                      ~^~~~~~~~~
shymbulak.cpp: In function 'void dfs_diametr(long long int, long long int)':
shymbulak.cpp:57:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |     for (i=0;i<g[x].size();++i){
      |              ~^~~~~~~~~~~~
shymbulak.cpp:68:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |     for (i=1;i<tmp.size();++i){
      |              ~^~~~~~~~~~~
shymbulak.cpp: In function 'void posledniy_dfs(long long int, long long int)':
shymbulak.cpp:98:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |     for (i=0;i<g[x].size();++i){
      |              ~^~~~~~~~~~~~
shymbulak.cpp:111:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |         for (i=1;i<tmp.size();++i){
      |                  ~^~~~~~~~~~~
shymbulak.cpp:122:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |         for (i=0;i<xixi.size();++i){
      |                  ~^~~~~~~~~~~~
shymbulak.cpp:137:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  137 |         for (i=1;i<tmp.size();++i){
      |                  ~^~~~~~~~~~~
shymbulak.cpp:135:19: warning: unused variable 'bla' [-Wunused-variable]
  135 |         int sum=0,bla=0;
      |                   ^~~
shymbulak.cpp: At global scope:
shymbulak.cpp:165:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  165 | main(){
      |      ^
shymbulak.cpp: In function 'int main()':
shymbulak.cpp:199:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  199 |     for (i=0;i<otvet.size();++i){
      |              ~^~~~~~~~~~~~~
shymbulak.cpp:200:13: warning: unused variable 'vershina' [-Wunused-variable]
  200 |         int vershina = otvet[i];
      |             ^~~~~~~~
shymbulak.cpp:167:13: warning: unused variable 'j' [-Wunused-variable]
  167 |     int n,i,j;
      |             ^
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 94316 KB Output is correct
2 Correct 64 ms 94316 KB Output is correct
3 Correct 64 ms 94316 KB Output is correct
4 Correct 64 ms 94316 KB Output is correct
5 Correct 64 ms 94444 KB Output is correct
6 Correct 64 ms 94316 KB Output is correct
7 Correct 64 ms 94316 KB Output is correct
8 Correct 65 ms 94316 KB Output is correct
9 Correct 64 ms 94316 KB Output is correct
10 Correct 65 ms 94316 KB Output is correct
11 Correct 65 ms 94316 KB Output is correct
12 Correct 64 ms 94328 KB Output is correct
13 Correct 65 ms 94316 KB Output is correct
14 Correct 65 ms 94344 KB Output is correct
15 Correct 66 ms 94316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 94572 KB Output is correct
2 Correct 66 ms 94444 KB Output is correct
3 Correct 65 ms 94444 KB Output is correct
4 Correct 67 ms 94444 KB Output is correct
5 Correct 70 ms 94828 KB Output is correct
6 Correct 77 ms 95084 KB Output is correct
7 Correct 71 ms 94956 KB Output is correct
8 Correct 70 ms 94828 KB Output is correct
9 Correct 69 ms 94828 KB Output is correct
10 Correct 69 ms 94828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 274 ms 106348 KB Output is correct
2 Correct 271 ms 106476 KB Output is correct
3 Correct 179 ms 114284 KB Output is correct
4 Correct 189 ms 106092 KB Output is correct
5 Correct 189 ms 106092 KB Output is correct
6 Correct 507 ms 117228 KB Output is correct
7 Correct 370 ms 111340 KB Output is correct
8 Correct 314 ms 117868 KB Output is correct
9 Correct 325 ms 118124 KB Output is correct
10 Correct 290 ms 119660 KB Output is correct
11 Correct 348 ms 121900 KB Output is correct
12 Correct 360 ms 123016 KB Output is correct