210. Beloved Sonstime limit per test: 2 sec.
memory limit per test: 65536 KBinput: standard
output: standard
Once upon a time there lived a king and he had N sons. And the king wanted to marry his beloved sons on the girls that they did love. So one day the king asked his sons to come to his room and tell him whom do they love.
But the sons of the king were all young men so they could not tell exactly whom they did love. Instead of that they just told him the names of the girls that seemed beautiful to them, but since they were all different, their choices of beautiful girls also did not match exactly.
The king was wise. He did write down the information that the children have provided him with and called you, his main wizard.
"I want all my kids to be happy, you know," he told you, "but since it might be impossible, I want at least some of them to marry the girl they like. So please, prepare the marriage list."
Suddenly you recalled that not so long ago the king told you about each of his sons, so you knew how much he loves him. So you decided to please the king and make such a marriage list that the king would be most happy. You know that the happiness of the king will be proportional to the square root of the sum of the squares of his love to the sons that would marry the girls they like.
So, go on, make a list to maximize the king’s happiness.
Input
The first line of the input file contains N — the number of king’s sons (1 ≤ N ≤ 400). The second line contains N integer numbers Ai ranging from 1 to 1000 — the measures of king’s love to each of his sons.
Next N lines contain lists of king’s sons’ preferences — first Ki — the number of the girls the i-th son of the king likes, and then Ki integer numbers — the girls he likes (all potentially beautiful girls in the kingdom were numbered from 1 to N, you know, beautiful girls were rare in those days).
Output
Output N numbers — for each son output the number of the beautiful girl he must marry or 0 if he must not marry the girl he likes.
Denote the set of sons that marry a girl they like by L, then you must maximize the value of
Sample test(s)
Input 4
1 3 2 4
4 1 2 3 4
2 1 4
2 1 4
2 1 4
Output
2 1 0 4
看上去好像是一个最优匹配的问题,可以很显然将所有孩子排个序,然后一个个匹配过去就可以了囧。。
MS那个八中的OJ不能上了囧。。只好A SGU了囧。。
Code:
#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstdlib>#include<cstring>#include<string>#include<vector>#include<set>#include<queue>#include<map>#define rep(i,n) for(int i=0;i<n;i++)#define For(i,a,b) for(int i=a;i<=b;i++)#define tr(i,x) for(typeof(x.begin()) i=x.begin();i!=x.end();i++)#define all(x) x.begin(),x.end()#define pb push_backusing namespace std;const int inf=~0U>>1,maxn=400;typedef vector<int> vi;typedef vector<vi> vvi;typedef pair<int,int> ii;vi E[maxn];int Link[maxn],n;bool Vis[maxn]={0};bool dfs(int x){ if(Vis[x]) return false; Vis[x]=true; tr(i,E[x])if(Link[*i]==-1||dfs(Link[*i])) return Link[*i]=x,true; return false;}int A[maxn];bool cmp(const int&a,const int&b){ return A[a]>A[b];}int main(){ //freopen("in","r",stdin); int P[maxn]; cin>>n; rep(i,n)cin>>A[i],P[i]=i; rep(i,n) { int t,x;cin>>t; while(t–)cin>>x,E[i].pb(x-1); } rep(i,n) Link[i]=-1; sort(P,P+n,cmp); rep(i,n) { memset(Vis,0,sizeof(Vis)); dfs(P[i]); } int Ans[maxn]={0}; rep(i,n) Ans[Link[i]]=i+1; rep(i,n) cout<<Ans[i]<<" "; cout<<endl;}
啊……我也上不去了……
回复中国脑筋:晕。。我要去做一个杭外的OJ。。
回复WJBZBMR:我就做做省选题吧……(我觉得HYOJ应该在(JS)省选前上线)
回复中国脑筋:Orz神牛。。我NOIP悲剧了省选去不了囧。。
回复WJBZBMR:话说今天下午7点就上线了……您是江苏的?
回复中国脑筋:饿。。我是浙江的囧。。
回复WJBZBMR:NOIP杯具NM
回复ws1192:你TM看看时间