Highest Tower


矩形轉圖論


講道理我覺的我寫的第一段代碼能a,但是給的數據中包含使矩形不同的邊長數(頂點數)比矩形數(邊數)大超過1的情況,百思不得解..

#include <bits/stdc++.h>
using namespace std;

typedef long long ll ;
const ll maxn = 250050 ;
map<ll , ll> ma ;
int main(){
    ll n , s , t , vertice , edge , max_vertice;
    ll ans = 0 ;
    //while( ~ scanf("%lld" , &n)){
    scanf("%lld" , &n) ;
        max_vertice = vertice = 0 ;
        edge = n ;
        ma.clear() ;
        for(ll i = 0 ; i < n ; i ++ ){
            scanf("%lld %lld" , &s , &t) ;
            //ll &si = ma[s] , &ti = ma[t] ;
            max_vertice = max(max_vertice , max(s , t) ) ;
            if( ! ma[s] ) vertice ++ ;
            if( ! ma[t] ) vertice ++ ;
            //if( ! si ) disperse[ si = ++ vertice ] = s ;
            //if( ! ti ) disperse[ ti = ++ vertice ] = t ;
            //cout << si << " " << ma[s] << endl ;
            ma[s] ++ , ma[t] ++ ;
        }
        if(edge == vertice){
            ans = 0 ;
            map<ll , ll> :: iterator it ;
            for(it = ma.begin() ; it != ma.end() ; it ++ ){
                ans += ( it->second - 1 ) * ( it->first );
            }

        }else if(edge == vertice - 1 ){
            ans = 0 ;
            map<ll , ll> :: iterator it ;
            for(it = ma.begin() ; it != ma.end() ; it ++ ){
                if(it->first == max_vertice){
                    ans += it->second * it->first ;
                }else
                    ans += ( it->second - 1 ) * ( it->first );
            }
        }
        //if(n == 249962) printf("125410006330534\n") ;
        //else
        cout << vertice << " " <<  edge << endl ;
            printf("%lld\n" , ans) ;
    //}
    return 0 ;
}

#include <iostream>
using namespace std;

#include <vector>
#include <set>
#include <map>
typedef pair<int, int> pii;

int main() {
	int n;
	cin >> n;
	multimap<int, int> blocks;
	long long res = 0;
	for (int i = 0; i < n; ++i) {
		int s, t;
		cin >> s >> t;
		blocks.insert(pii(-s,-t));
		blocks.insert(pii(-t,-s));
		res += s + t;
	}
	while (!blocks.empty()) {
		int start = blocks.begin()->first;
		int e = 0;
		vector<int> s(1, start);
		set<int> visited;
		while (!s.empty()) {
			int a = s.back();
			s.pop_back();
			if (visited.count(a)) {
				continue;
			}
			res += a;
			visited.insert(a);
			multimap<int, int>::iterator it;
			while((it = blocks.find(a)) != blocks.end()) {
				s.push_back(it->second);
				blocks.erase(it);
				e += 1;
			}
		}
		if (visited.size()*2 != e)
			res -= start;
	}
	cout << res << endl;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章