hdu5398 GCD Tree(lct)

时间:2023-03-09 07:29:58
hdu5398 GCD Tree(lct)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

GCD Tree

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 84    Accepted Submission(s): 38

Problem Description
Teacher Mai has a graph with n vertices numbered from 1 to n. For every edge(u,v), the weight is gcd(u,v). (gcd(u,v) means the greatest common divisor of number u and v).

You need to find a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is maximized. Print the total weight of these edges.



Input
There are multiple test cases(about 105).

For each test case, there is only one line contains one number n(1≤n≤105).



Output
For each test case, print the answer.



Sample Input
1
2
3
4
5



Sample Output
1
2
4
5

LCT维护最大生成树,从小到大不断加入数的时候,先和它最大的约数连边,这样就已经变成一棵树了,然后从大到小依次查询其他因子,每次查找到这个数的路径上的最小值,然后砍断这条边,把i和这次查询的因子相连,因为砍掉的边的权值一定不会超过新加的边,所以每次都在增大,这样能够保证最优

队友随手过了这题。23333333

我的写法是把边权搞成一个点,其实不这样也行,找出最小的点,然后看一下在路径上和这个点相邻的点。

 /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author xyiyy @https://github.com/xyiyy
*/ #include <iostream>
#include <fstream> //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype> using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define mp(X, Y) make_pair(X,Y)
#define pb(X) push_back(X)
#define rep(X, N) for(int X=0;X<N;X++)
#define rep2(X, L, R) for(int X=L;X<=R;X++)
#define dep(X, R, L) for(int X=R;X>=L;X--)
#define clr(A, X) memset(A,X,sizeof(A))
#define IT iterator
#define ALL(X) (X).begin(),(X).end()
#define PQ std::priority_queue
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<PII> VII;
typedef vector<int> VI; const int MAXN = ;
int pre[MAXN<<], ch[MAXN<<][], rev[MAXN<<];
int key[MAXN<<];
int lx[MAXN<<],rx[MAXN<<];
int mx[MAXN<<]; void push_down(int r) {
if(!r)return;
if (rev[r]) {
rev[ch[r][]] ^= ;
rev[ch[r][]] ^= ;
swap(ch[r][], ch[r][]);
rev[r] ^= ;
}
} void push_up(int x) {
int l = ch[x][],r = ch[x][];
mx[x] = x;
if(key[mx[l]]<key[mx[x]])mx[x] = mx[l];
if(key[mx[r]]<key[mx[x]])mx[x] = mx[r];
} void rotate(int x, int d) {
const int y = pre[x];
ch[y][!d] = ch[x][d];
if (ch[x][d])pre[ch[x][d]] = y;
pre[x] = pre[y];
if (ch[pre[y]][] == y)ch[pre[x]][] = x;
else if (ch[pre[y]][] == y)ch[pre[x]][] = x;
pre[y] = x;
ch[x][d] = y;
push_up(y);
} bool _splay_parent(int x,int &y){
return (y = pre[x])!= && (ch[y][] == x || ch[y][] == x);
} void splay(int x,int goal) {
push_down(x);
for (int y,z;_splay_parent(x,y);) {
//cout<<x<<" "<<y<<endl;
if(_splay_parent(y,z)){
push_down(z);push_down(y);push_down(x);
int d = y == ch[z][];
if(x == ch[y][d])rotate(x,d^),rotate(x,d);
else rotate(y,d),rotate(x,d);
}else {
push_down(y),push_down(x);
rotate(x, x == ch[y][]);break;
}
}
push_up(x);
} int access(int u) {
int v = ;
for (; u; u = pre[u]) {
splay(u,);
ch[u][] = v;
push_up(v = u);
}
return v;
} void makeroot(int x) {
rev[access(x)] ^= ;
splay(x,);
} void link(int x, int y) {
makeroot(x);
pre[x] = y;
} void cut(int x, int y) {
makeroot(x);
access(y);
splay(y,);
pre[ch[y][]] = ;
ch[y][] = ;
push_up(y);
} void Init(int n) {
for (int i = ; i < n; i++) {
pre[i] = ch[i][] = ch[i][] = ;
key[i] = INF;
mx[i] = ;
}
}
void debug(int x){ }
int query(int x, int y) {
makeroot(x);
access(y);
splay(y,);
return mx[y];
} vector<int> vec[MAXN];
int ans[MAXN]; class hdu5398 {
public:
void solve(std::istream &in, std::ostream &out) {
rep2(i, , MAXN-) {
vec[i].pb();
for (int j = ; j * j <= i; j++) {
if (i % j == ) {
vec[i].pb(j);
if (i / j != j)vec[i].pb(i / j);
}
}
sort(vec[i].begin(),vec[i].end());
}
Init(MAXN<<);
ans[] = ;
rep2(i, , MAXN-) {
int sz = vec[i].size();
int y = vec[i][sz - ];
ans[i] = ans[i - ];
link(i, MAXN + i);
rx[MAXN + i] = i;
link(y, MAXN + i);
lx[MAXN + i] = y;
key[MAXN + i] = y;
ans[i] += y;
dep(j, sz - , ) {
y = vec[i][j];
int x = query(y, i);
cut(x,lx[x]);
cut(x,rx[x]);
link(y, x);
link(i, x);
ans[i] -= key[x];
key[x] = y;
lx[x] = y;
rx[x] = i;
ans[i] += y;
}
}
int n;
while(in>>n){
out<<ans[n]<<endl;
} }
}; int main() {
std::ios::sync_with_stdio(false);
std::cin.tie();
hdu5398 solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
solver.solve(in, out);
return ;
}