1139 First Contact (I string rotation number, ??? cool four hours)

 Roughly meaning of the title is A fancy B, A to C by a friend of the same sex, if C know D, and D is the same sex friend B, then B of A can pursue.

Method One: DFS violence enumeration method, cool ~ ~.

 1 #include<iostream>
 2 #include<vector>
 3 #include<unordered_map>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 struct node {
 8     int a,b;
 9 };
10 const int maxn = 310;
11 int n,m,k;
12 
13 vector<int> G[maxn],path;
14 bool visited[maxn] = {false};
15 vector<node> ans;
16 bool cmp(node& a,node& b) {
17     if(a.a != b.a) return a.a < b.a;
18     else return a.b < b.b;
19 }
20 unordered_map<string,int> IDtoInt;
21 unordered_map<int,string> IntToID;
22 int num = 1;
23 int getID(string id) {
24     if(IDtoInt.count(id) == 0) {
25         IDtoInt[id] = num;
26         IntToID[num++] = id;
27     }
28     return IDtoInt[id];
29 }
30 
31 void DFS(int now,int end,int len) {
32     if(len == 4) {
33         if(now == end) {
34             path.push_back(end);
35             if(IntToID[path[0]].size() == IntToID[path[1]].size()  &&IntToID[path[2]].size() == IntToID[path[3]].size() )
36                 ans.push_back({abs(stoi(IntToID[path[1]])),abs(stoi(IntToID[path[2]]))});
37             path.pop_back();
38         }
39         return ;
40     }
41     if(now == end && len < 4) return;
42     visited[now] = true;
43     path.push_back(now);
44     for(int i = 0; i < G[now].size(); ++i) {
45         if(visited[G[now][i]] == false)
46             DFS(G[now][i],end,len+1);
47     }
48     visited[now] = false;
49     path.pop_back();
50 }
51 int main() {
52     scanf("%d%d",&n,&m);
53     string a,b; //必须考虑 -0000,0000的情况,所以用string
54     for(int i = 0; i < m; ++i) {
55         cin>>a>>b;
56         int u = getID(a),v = getID(b);
57         G[u].push_back(v);
58         G[v].push_back(u);
59     }
60     scanf("%d",&k);
61     while(k--) {
62         cin>>a>>b;
63         ans.clear();
64         DFS(IDtoInt[a],IDtoInt[b],1);
65         sort(ans.begin(),ans.end(),cmp);
66         printf("%d\n",ans.size());
67         for(int i = 0; i < ans.size(); ++i) printf("%04d %04d\n",ans[i].a,ans[i].b);
68     }
69     return 0;
70 }

Method Two:

Establishing a corresponding relationship with the ID number with the map.

Adjacency table holds a pair of vertices of friends.

Adjacency matrix, all save a person's same-sex friends.

If same-sex friends A is C, B is the same-sex friend D, if C and D is a friend, then A can pursue B.

important point:

1, the number must be from 1 to start, because the ID given possible illegal, the corresponding number of 0 indicates not find this person, or test point 5 can not pass.

2, pay attention to the input as a -0000, -0000 must be accepted with string

 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 #include<unordered_map>
 5 using namespace std;
 6 const int maxn = 320;
 7 struct node {
 8     int a,b;
 9 };
10 int n,m,k,G[maxn][maxn];
11 vector<int> adj[maxn];
12 bool cmp(node a,node b) {
13     if(a.a != b.a)  return a.a < b.a;
14     the else  return ab & < BB;
 15  }
 16 unordered_map < String , int > IDtoInt;
 . 17 unordered_map < int , String > IntToID;
 18 is  int NUM = . 1 ; // write num = 0, the lead test points 5 can not pass, because the input illicit IDtoInt [ID] = 0, so the number num from a legitimate start of ID 
. 19  int the getID ( String ID) {
 20 is      IF (! IDtoInt.count (ID)) {
 21 is          IDtoInt [ID] = num;
 22 is          IntToID [num ++] = the above mentioned id;
 23     }
 24      return IDtoInt [ID];
 25  }
 26 is  int main () {
 27      Fill (G [ 0 ], G [ 0 ] + MAXN MAXN *, 0 );
 28      CIN >> >> n- m;
 29      String A, B ;
 30      for ( int I = 0 ; I <m; ++ I) {
 31 is          CIN >> a >> B; // the reason for this is that there are error-prone reading point: -0000 
32          int U = the getID (a) , V = the getID (B);
 33 is          G [U] [V] = G [V] [U] = . 1 ; // friendship 
34         IF (a.size () == b.size ()) { // sex friend relationship 
35              ADJ [U] .push_back (V);
 36              ADJ [V] .push_back (U);
 37 [          }
 38 is      }
 39      CIN> > K;
 40      the while (K-- ) {
 41 is          CIN >> A >> B;
 42 is          Vector <Node> ANS;
 43 is          int U = IDtoInt [A], V = IDtoInt [B];
 44 is          for ( int I = 0 ; I <ADJ [u] .size (); I ++) { // find u pals C 
45              int C = adj[u][i];
46             for(int j = 0; j < adj[v].size(); ++j) {//找v的同性朋友 D
47                 int d = adj[v][j];
48                 if(v == c || u == d) continue;
49                 if(G[c][d]) ans.push_back({abs(stoi(IntToID[c])),abs(stoi(IntToID[d]))});
50             }
51         }
52         sort(ans.begin(),ans.end(),cmp);
53         printf("%d\n",ans.size());
54         for(int i = 0; i < ans.size(); ++i)
55             printf("%04d %04d\n",ans[i].a,ans[i].b);
56     }
57     return 0;
58 }

Method three:

Open very large array, direct mapping ID, without using a map.

 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 
 5 using namespace std;
 6 const int maxn = 10000;
 7 struct node {
 8     int a,b;
 9 };
10 int n,m,k,G[maxn][maxn]= {0};
11 vector<int> adj[maxn];
12 bool cmp(node a,node b) {
13     if(a.a != b.a)  return a.a < b.a;
14     the else  return ab & < BB;
 15  }
 16  int main () {
 . 17      CIN >> >> n- m;
 18 is      String A, B;
 . 19      for ( int I = 0 ; I <m; I ++ ) {
 20 is          CIN >> A> > B; // the reason for this is that there are error-prone reading point: -0000 
21 is          int U = ABS (Stoi (a)), V = ABS (Stoi (B));
 22 is          G [U] [V] = G [V] [U] = . 1 ;
 23 is          IF (a.size () == b.size ()) { // sex friends 
24              ADJ [U] .push_back (V);
 25             adj[v].push_back(u);
26         }
27     }
28     cin>>k;
29     while(k--) {
30         cin>>a>>b;
31         vector<node> ans;
32         int u = abs(stoi(a)),v = abs(stoi(b));
33         for(int i = 0; i < adj[u].size(); i++) {
34             int c = adj[u][i];
35             for(int j = 0; j < adj[v].size(); j++) {
36                 int d = adj[v][j];
37                 if(v == c || u == d) continue;
38                 if(G[c][d]) ans.push_back({c,d});
39             }
40         }
41         sort(ans.begin(),ans.end(),cmp);
42         printf("%d\n",ans.size());
43         for(int i = 0; i < ans.size(); i++) {
44             printf("%04d %04d\n",ans[i].a,ans[i].b);//%04d为易错点
45         }
46     }
 47      return  0 ;
48 }

 

Guess you like

Origin www.cnblogs.com/keep23456/p/12498957.html