考察并查集。發現并查集什么都不會呀。。。
查看了大神的代碼感覺自己太弱了 http://blog.csdn.net/sunbaigui/article/details/8657148
代碼照抄的 就是注釋了下。留著當筆記吧。 學習了下map set 迭代器 的使用 還是有點不熟練,還要多練。。
感覺這里開始PAT題的類型突然變了。。
#include <iostream>#include <string>#include <vector>#include <map>#include <set>#include <algorithm>#define MAX 1010using namespace std;struct Call { string p1, p2; int time;};struct Node { int weight, parent;};struct Gang { int head, num, sum, maxw; Gang() { head = -1; num = 0; sum = 0; maxw = -1; }};vector <Call> list;vector <Node> p; //并查集set <string> name;map <string, int> str2int;map <int, string> int2str;int NameNum;int N, threthold;void InitSet() { p.resize(NameNum); for (int i = 0; i < NameNum; i++) { p[i].parent = i; p[i].weight = 0; //當父母為自己時為根節點 }}//結點壓縮// ① ①// | / /// ② 壓縮成 ② ③// |// ③void ComPRessSet(int top, int x) { //路徑壓縮 if (top != p[x].parent) {//不是并查集的根結點 CompressSet(top, p[x].parent); //繼續壓縮 p[x].parent = top; }}int FindSet(int x) {//查找根節點 if (x != p[x].parent) { //不是根結點 int top = FindSet(p[x].parent); //top為根結點 CompressSet(top, x); //壓縮結點 } return p[x].parent;//返回根節點}void UnionSet(int x, int y) { int a = FindSet(x); int b = FindSet(y); p[a].parent = b;}bool cmp(Gang g1, Gang g2) { return int2str[g1.head] < int2str[g2.head];}int main() { cin >> N >> threthold; Call temp; for (int i = 0; i < N; i++) { cin >> temp.p1 >> temp.p2 >> temp.time; list.push_back(temp); name.insert(temp.p1); name.insert(temp.p2); } NameNum = name.size(); //名字轉換為編號 set<string>::iterator it; int id = 0; for (it = name.begin(); it != name.end(); it++, id++) { str2int[*it] = id; int2str[id] = *it; } InitSet(); for(int i = 0; i < list.size(); i++){ int u = str2int[list[i].p1]; int v = str2int[list[i].p2]; p[u].weight += list[i].time; p[v].weight += list[i].time; UnionSet(u, v); }#ifdef _DEBUG for (int i = 0; i < p.size(); i++) { cout << int2str[i] << " " << int2str[p[i].parent] << " " << int2str[FindSet(i)] << endl; }#endif map <int, Gang> allset; // fitst = int second = Gang map <int, Gang>::iterator it2; for (int i = 0; i < NameNum; i++) { int top = FindSet(i); it2 = allset.find(top);//查找根節點的幫會集合 if (it2 != allset.end()) { allset[top].sum += p[i].weight; allset[top].num++; if (p[i].weight > allset[top].maxw) { allset[top].maxw = p[i].weight; allset[top].head = i; } } else {//沒有 新建幫會 Gang temp; temp.head = i; temp.maxw = p[i].weight; temp.num = 1; temp.sum = p[i].weight; allset[top] = temp; } } vector <Gang> gang; for (it2 = allset.begin(); it2 != allset.end(); it2++) { if (it2->second.sum > 2 * threthold && it2->second.num > 2) gang.push_back(it2->second); } sort(gang.begin(), gang.end(), cmp); cout << gang.size() << endl; for (int i = 0; i < gang.size(); i++) { cout << int2str[gang[i].head] << " " << gang[i].num << endl; } system("pause"); return 0;}
新聞熱點
疑難解答