很妙的一道题。

由于本人过于zz,不会这道题,通过厚颜无耻翻阅题解无数终于懂了这道题,所以这里转载一位神仙的blog

没有看懂?没事,再来一篇

这题个人认为主要在于转化题意和建图,这两点想通了应该就不难了。

转化题意:每个长只要互不相等即可,不管什么严格大于。

建图:限制了每个长只能被选一次,另一边作为价值。有没有条件限制感觉和上一篇博客相像?长和宽代表的两个点之间建一条边,从长到宽的方向代表以长为底,反向则以宽为底。然后限制每个点出度最多为1.

图建出来,我们只要定向时满足每个点至多出度为1,就可以。题目有保证了有解,也就是不会出现有点迫不得已出度超过1的情况,所以我们只要放心讨论怎么怎么定向使得入度对应的点权最大即可。

然后发现并不需要定向,按照上面地址里的方法讨论连通性即可。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#define dbg(x) cerr << #x << " = " << x <<endl
#define dbg2(x,y) cerr<< #x <<" = "<< x <<" "<< #y <<" = "<< y <<endl
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
template<typename T>inline T _min(T A,T B){return A<B?A:B;}
template<typename T>inline T _max(T A,T B){return A>B?A:B;}
template<typename T>inline char MIN(T&A,T B){return A>B?(A=B,):;}
template<typename T>inline char MAX(T&A,T B){return A<B?(A=B,):;}
template<typename T>inline void _swap(T&A,T&B){A^=B^=A^=B;}
template<typename T>inline T read(T&x){
x=;int f=;char c;while(!isdigit(c=getchar()))if(c=='-')f=;
while(isdigit(c))x=x*+(c&),c=getchar();return f?x=-x:x;
}
const int N=+;
int n;
struct thxorz{int to,nxt;}G[N<<];
int Head[N<<],cnt,tot,edge,vertex,maxv;
ll ans;
inline void Addedge(int x,int y){
G[++tot].to=y,G[tot].nxt=Head[x],Head[x]=tot;
G[++tot].to=x,G[tot].nxt=Head[y],Head[y]=tot;
}
int deg[N<<],val[N<<],vis[N<<];
map<int,int> mp;
#define y G[j].to
inline int dfs(int x){
vis[x]=;++vertex;edge+=deg[x];
MAX(maxv,val[x]);ans+=(deg[x]-)*1ll*val[x];
for(register int j=Head[x];j;j=G[j].nxt)if(!vis[y])dfs(y);
}
#undef y
int main(){//freopen("test.in","r",stdin);//freopen("test.ans","w",stdout);
read(n);
for(register int i=,x,y;i<=n;++i){
read(x),read(y);
int cx=mp.find(x)==mp.end()?mp[x]=++cnt:mp[x];
int cy=mp.find(y)==mp.end()?mp[y]=++cnt:mp[y];
Addedge(cx,cy);++deg[cx],++deg[cy],val[cx]=x,val[cy]=y;
}
for(register int i=;i<=cnt;++i)if(!vis[i]){
edge=vertex=maxv=;dfs(i);edge>>=;
if(edge<vertex)ans+=maxv;
}
return printf("%lld\n",ans),;
}

总结:这种神仙建图题没有办法总结。只能积累技巧,善于转化问题。

05-23 03:19