#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include < math.h> #include <algorithm> #define N 5010using namespace Std;int num (char *s) {int ans; if (s[0]>= ' 2 ' && s[0]<= ' 9 ') {ans = s[0]-48; } else if (s[0]== ' T ') ans = 10; else if (s[0]== ' J ') ans = 11; else if (s[0]== ' Q ') ans = 12; else if (s[0]== ' K ') ans = 13; else if (s[0]== ' A ') ans = 1; return ans;} int main () {int t; scanf ("%d", &t); int cnt=1; int n; int I, j, K; Char s[10][5]; while (t--) {scanf ("%d", &n); for (i=0; i<n; i++) {scanf ("%s", S[i]); }//data initialization char STR[10]; memset (str, ' n ', sizeof (str)); for (i=0; i<n; i++) {str[i]=i+48; } str[n]= ' + '; int ans=0; do {int cur; BOOL Flag=true; for (i=0; i<n; i++) {if (i==0) cur = num (s[str[i]-48]); else {int dd=num (s[str[i]-48]); if (dd = = cur) {flag=false; break; } else cur = dd; }} if (Flag==true) ans++; } while (Next_permutation (str, str+n)); printf ("Case #%d:%d\n", cnt++, ans); } return 0;}
Topic 2 I didn't see it ~ ~ ~
Topic 3: The Xiaoice of gossip Time limit: 2000msSingle Point time limit: 1000msMemory Limit: 256MBDescribe
Xiaoice was a gossip, and recently she was interested in a social networking site.
Since Xiaoice is a robot, it is certainly possible to quickly figure out the user's information in this social networking site.
She found that there were n users in the social networking site, and that users and users could interact with each other. Xiaoice determine the degree of intimacy between each user based on the number and content of interactions between users. A non-negative intimacy, if greater than 0, indicates that the two users are friend relationships. Since this site is active, Xiaoice will constantly update the intimacy between users.
Due to privacy protection, Xiaoice cannot know the exact gender of each user, but as a clever AI, Xiaoice can guess the gender through the behavior of each user. Of course, this speculation is inaccurate, Xiaoice may change the judgment of a user.
Xiaoice want to know how much gossip this social network is. The definition of gossip is the sum of the affinity between all heterosexual friends in a social network. Can you help her?
Input
The first line is an integer t that represents the number of data groups. Next is the T-group data, and the format of each group of data is as follows:
The first line is three integers n, M, Q, respectively, the number of users, the initial friend logarithm, the operand.
The second line is the number of n spaces separated, the number of I indicates the gender of user I, expressed in 0 or 1.
The next M-line, three numbers per line x, Y, Z, represents the initial state of intimacy between user X and user y is Z. In addition, the intimacy between users is initially 0.
Next is the Q line, where each line is one of the following three operations:
1. "1 x": Change the gender of user X
2. "2 x y z": Change the degree of intimacy between user X and user y as Z
3. "3": Ask the Gossip degree
Output
For each set of data, first output one line "case #X:" and X is the test data number.
Next, for each query, the output line contains the gossip of the query.
Data range
1≤t≤20
1≤x, Y≤n
0≤z≤100000
Small Data
1≤n, m≤100
1≤q≤1000
Big Data
1≤n, M, q≤100000
-
Sample input
-
13 2 80 1 01 2 11 3 131 11 232 2 3 231 23
-
Sample output
-
Case #1:1223
Algorithm analysis: Small data time is very simple, the establishment of two-dimensional matrix storage, every time the gossip value of the time the violence of the map, statistical calculation on it.
Code
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include < math.h> #include <algorithm> #define N 5010using namespace Std;int n, m, q; Three integers n, M, Q, respectively, indicates the number of users, the initial friend logarithm, the operand. /* The first line is three integers n, M, Q, each representing the number of users, the initial friend logarithm, the operand. The second line is the number of n spaces separated, the number of I indicates the gender of user I, expressed in 0 or 1. The next M-line, three numbers per line x, Y, Z, represents the initial state of intimacy between user X and user y is Z. In addition, the intimacy between users is initially 0. Next is the Q line, where each line is one of the following three operations: 1. "1 x": Change the gender of User X 2. "2 x y z": Changes the intimacy between user X and User y as Z3. "3": Ask the Gossip degree */int map[110][110];int Main () {int t; scanf ("%d", &t); int cnt=1; int sex[110]; while (t--) {memset (map, 0, sizeof (map)); scanf ("%d%d%d", &n, &m, &q); for (int i=1; i<=n; i++) scanf ("%d", &sex[i]); int x, y, Z; for (int i=0; i<m; i++) {scanf ("%d%d%d", &x, &y, &z); Map[x][y]=z; Map[y][x]=z; } printf ("Case #%d:\n", cnt++); while (q--) {int dd; scanf ("%d", &DD); if (dd==1) {scanf ("%d", &x); SEX[X] = sex[x]==0?1:0; Modify Gender} else if (dd==2) {scanf ("%d%d%d", &x, &y, &z);//change the intimacy between user X and User y For z map[x][y]=z; Map[y][x]=z; } else{//Ask gossip degree: the affinity between all heterosexual friends in social networks and long long ans=0; /* for (int i=1; i<=n; i++) printf ("%d", sex[i]); printf ("\ n"); */for (int i=1, i<=n; i++) {for (int j=i+1; j<=n; j + +) {if (map[i][j]>0 && (Sex[i]+sex[j]) ==1) {ans = ans + MAP[I][J]; map[i][j]=0; map[j][i]=0; Break Relationship}}} printf ("%lld\n", ans); }}} return 0;}
how to deal with Big data:
Since it is big data, it can be imagined that when the gossip value every time, it is impossible to ask every time to re-calculate the entire graph of the gossip value.
This must be timed out! So our idea is that after the initial construction of the map, it is good to count the whole Tujantu of the beginning of the gossip value, later if you encounter
If asked, the direct output, if encountered to modify the information of the picture will be dynamically modified the entire graph of the gossip value ans.
1. Assuming that the gender of the modified node x is encountered, the gender modification of x affects only the nodes adjacent to it.
Divided into four cases: Ori (x) =0, cur (x) = 1; The original sex was 0, and now it's 1.
Nodes adjacent to X are assumed to be y:sex (y) =0 or sex (y) =1
In other words: Sex[cur (x)] + sex[y] = = (1+0=1) or (1+1=2)
Ori = 1, cur = 0;
Sex[cur (x)] + sex[y] = = (0+0=0) or (0+1=1)
After the addition of the value of 1, indicating that X modified after the two sexes opposite, gossip value ans to add weight (x-y) weight.
The added value is 0 or 2, indicating that X is modified to the opposite sex, the gossip value ans to subtract (x-y) weight.
2. If you modify the weight between x-y nodes to Z, first look at the X-y node before the gossip value (the initial default is 0: indicates none).
If not, add it, and if so, modify it. If both sexes are the same, modifying the weights will not affect the current
The gossip value ans, if x-y sex is different then you have to consider how to change the gossip value ans, this is very simple, specifically not detailed.
Code:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include < math.h> #include <vector> #include <algorithm> #define N 100000+10using namespace Std;int n, m, q; Three integers n, M, Q, respectively, indicates the number of users, the initial friend logarithm, the operand. struct node{int to; int W;} Temp;int Main () {int t; scanf ("%d", &t); int cnt=1; int sex[n]; while (t--) {vector<node>qm[n]; Long Long ans=0; scanf ("%d%d%d", &n, &m, &q); for (int i=1; i<=n; i++) scanf ("%d", &sex[i]); int x, y, Z; for (int i=0; i<m; i++) {scanf ("%d%d%d", &x, &y, &z); if (sex[x]+sex[y]==1) ans = ans+z; Temp.to=y; Temp.w=z; Qm[x].push_back (temp); Temp.to=x; Qm[y].push_back (temp); Build} printf ("Case #%d:\n", cnt++); while (q--) {int dd; scanf ("%d", &DD); if (dd==1) {scanf ("%d", &x); SEX[X] = sex[x]==0?1:0; modify gender int len; Len=qm[x].size (); for (int i=0; i<len; i++) {if (sex[x]+sex[qm[x][i].to]==1) ans = ans + qm[x][i ].W; else if (sex[x]+sex[qm[x][i].to]==0) ans = ans-qm[x][i].w; else if (sex[x]+sex[qm[x][i].to]==2) ans = ans-qm[x][i].w; }} else if (dd==2) {scanf ("%d%d", &x, &y, &z);//Change the degree of intimacy between user X and user y as Z int ori=0, cur=0; int Len; Len=qm[x].size (); BOOL Flag=false; for (int i=0; i<len; i++) {if (qm[x][i].to = = y) {flag=true ; Ori = QM[X][I].W; Save a copy of the original value qm[x][i].w=z; Break }} if (Flag==false) {temp.to=y; temp.w=z; Qm[x].push_back (temp); } len=qm[y].size (); Flag=false; for (int i=0; i<len; i++) {if (qm[y][i].to = = x) {flag=true ; Qm[y][i].w=z; Break }} if (Flag==false) {temp.to=x; temp.w=z; Qm[y].push_back (temp); }//Modify cur =z; if (sex[x]+sex[y]==1) ans = ans + (Cur-ori); } else{//Ask gossip: the affinity between all heterosexual friends in the social network and printf ("%lld\n", ans); }}} return 0;}