zoukankan      html  css  js  c++  java
  • 2019CCSP A. 摘水果(拓扑排序)

    看到数据量1e2,直接暴力即可。每一轮暴力检查两棵树上度为0的点,寻找最优答案输出,同时更新deg数组。

    #include <bits/stdc++.h>
    #define N 405
    #define M 10005
    using namespace std;
    int n, head[N], ver[2 * M], Next[2 * M], tot = 0, deg[N], a[2 * N], b[2 * N];
    void add(int x, int y) {
    	ver[++tot] = y, Next[tot] = head[x], head[x] = tot;
    }
    bool chosen[N];
    int main() {
    	memset(deg, 0, sizeof(deg));
    	cin >> n;
    	for(int i = 1; i <= 2 * n; i++) {
    		cin >> a[i];
    	}
    	for(int i = 1; i <= 2 * n; i++) {
    		cin >> b[i];
    		add(i, b[i]);
    		deg[b[i]]++;
    	}
    	for(int t = 1; t <= n; t++) {
    		int val = 0, aa = 0, id1 = 0, id2 = 0;
    		for(int i = 1; i <= n; i++) {
    			for(int j = n + 1; j <= 2 * n; j++) {
    				if(!(deg[i] == 0 && deg[j] == 0 && chosen[i] == 0 && chosen[j] == 0)) 			continue;
    				int tmp = a[i] ^ a[j];
    				if(tmp > val) {
    					val = tmp;
    					aa = a[i];
    					id1 = i;
    					id2 = j;
    				} else if(tmp == val) {
    					if(a[i] > aa) {
    						aa = a[i];
    						id1 = i;
    						id2 = j;
    					} else if(a[i] == aa) {
    						if(i > id1) {
    							id1 = i;
    							id2 = j;
    						} else if(i == id1) {
    							if(j > id2) {
    								id2 = j;
    							}
    						}
    					}
    				}
    			}
    		}
    		cout << val << (t == n ? "" : " ");
    		//cout << id1 << " " << id2 << endl;
    		chosen[id1] = chosen[id2] = 1;
    		for(int i = head[id1]; i; i = Next[i]) {
    			int y = ver[i];
    			if(deg[y]) deg[y]--;
    		}
    		for(int i = head[id2]; i; i = Next[i]) {
    			int y = ver[i];
    			if(deg[y]) deg[y]--;
    		}
    	}
    }
    // 3              
    // 3 9 4 1 3 4
    // 0 0 2 0 4 4
    
  • 相关阅读:
    禁止后台运行
    图标的圆角和光晕效果和启动画面
    IOS 开发 有关iPhone程序的安装目录UUID 唯一标识
    NSOperation与performSelectorOnMainThread
    Java web开发学习规划
    JAVA类集
    java 四种xml操作方式的基本使用方法
    用JDOM操作XML文件
    java web 学习
    过去的,将来的
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/15409940.html
Copyright © 2011-2022 走看看