Test instructions: Calculates the total intimacy of all heterosexual pairs based on the degree of intimacy between each user.
Idea: Big data afraid too big, so use adjacency table to save this graph, in the input by the way of statistical intimacy sum. For changing gender: count The intimacy of the user's heterosexual friends, sum up the intimacy of the user's same-sex friends, make a difference, and update the total pro density. For change intimacy: to consider the case of changing to 0, that is, two users are not related, the picture to delete this edge, according to the increase or decrease of intimacy, the total degree of intimacy should be increased or decreased accordingly. Output directly when you want to query. This should be able to cope with all 3 of the situation. Personal thoughts!
1#include <iostream>2#include <map>3#include <stack>4#include <deque>5#include <queue>6#include <vector>7#include <Set>8#include <cstring>9#include <cstdio>Ten One using namespacestd; A Const intn=100005; - BOOLSex[n]; - intn,m,q; the Long LongQMD; - structnode - { - intname; + intQMD; - }; +vector< vector<node> >Lis; A at voidUpdateintHao) - { - Long Longtmp1=0; - Long LongTmp2=0; - for(intI=0; I<lis[hao].size (); i++) - { in if(Sex[lis[hao][i].name]!=sex[hao])//now the gender is different, then the past is the same. Which is not calculated, add this part -tmp1+=LIS[HAO][I].QMD; to Else //to subtract this part +tmp2+=LIS[HAO][I].QMD; - } theQMD + = tmp1-TMP2; * } $ intMain ()Panax Notoginseng { - //freopen ("Input.txt", "R", stdin); the intT, TMP1, TMP2, Tmp3, j=0; +Cin>>T; A while(t--) the { +Qmd=0; -Cin>>n>>m>>Q; $memset (Sex,0,sizeof(Sex)); $printf"Case #%d:\n",++j); - lis.resize (n); - for(intI=0; i<n; i++)//Sex the { -Cin>>Tmp1;Wuyisex[i]=Tmp1; the } - Wu for(intI=0; i<m; i++)//Degree of intimacy - { Aboutcin>>tmp1>>tmp2>>tmp3;tmp1--;tmp2--; $ - node tmp; -Tmp.name=TMP2; -Tmp.qmd=Tmp3; A + Lis[tmp1].push_back (TMP); theTmp.name=Tmp1; - Lis[tmp2].push_back (TMP); $ the if(sex[tmp1]!=SEX[TMP2]) theqmd+=Tmp3; the } the - for(intI=0; i<q; i++) in { theCin>>Tmp1; the if(tmp1==1)//Change Gender About { thecin>>tmp2;tmp2--; thesex[tmp2]= sex[tmp2]==1?0:1; the Update (TMP2); + } - Else if(tmp1==2)//Change of intimacy the {BayiCin>>tmp1>>tmp2>>Tmp3; thetmp1--;tmp2--; the - inttemp=-1; - for(intI=0; I<lis[tmp1].size (); i++)//The side is non-direction, so look for two times the { the if(lis[tmp1][i].name==tmp2) the { thetemp=LIS[TMP1][I].QMD; - if(tmp3==0) theLis[tmp1].erase (Lis[tmp1].begin () +i); the Else theLis[tmp1][i].qmd=Tmp3;94 Break; the } the } the for(intI=0; I<lis[tmp2].size (); i++)//The side is non-direction, so look for two times98 { About if(lis[tmp2][i].name==tmp1) - {101 if(tmp3==0)102Lis[tmp2].erase (Lis[tmp2].begin () +i);103 Else104Lis[tmp2][i].qmd=Tmp3; the Break;106 }107 }108 109 if(temp==-1)//to add new the {111 node tmp; theTmp.name=TMP2;113Tmp.qmd=Tmp3; the Lis[tmp1].push_back (TMP); theTmp.name=Tmp1; the Lis[tmp2].push_back (TMP);117 if(sex[tmp1]!=SEX[TMP2])118qmd+=Tmp3;119 } - Else if(sex[tmp1]!=SEX[TMP2])121qmd+= (tmp3-temp);122 }123 Else //Ask124cout<<qmd<<Endl;; the }126 }127 - return 0;129}not validated
Test data (2nd is official data):
Input:
2
3 2 4
0 1 0
1 2 1
1 3 1
3
2 1 2 0
2 1 3 0
3
3 2 8
0 1 0
1 2 1
1 3 1
3
1 1
1 2
3
2 2 3 2
3
1 2
3
Output:
Case #1:
1
0
Case #2:
1
2
2
3
The beauty of programming 2015 rounds of the second game C gossip Xiaoice