思路:城市数也就1000, 对于每次询问暴力bfs一下看一下有多少连通块就行了。答案就是联通块数减一。
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<map> 4 #include<set> 5 #include<iostream> 6 #include<cstring> 7 #include<algorithm> 8 #include<vector> 9 #include<cmath> 10 #include<queue> 11 12 #define inf 0x7fffffff 13 using namespace std; 14 typedef long long LL; 15 typedef pair<int, int> pr; 16 17 int n, m, k; 18 const int maxm = 1e6 + 5; 19 const int maxn = 1005; 20 struct edge{ 21 int u, v, nxt; 22 }e[maxm * 2]; 23 int head[maxn], tot = 1; 24 25 void addedge(int u, int v) 26 { 27 e[tot].u = u; 28 e[tot].v = v; 29 e[tot].nxt = head[u]; 30 head[u] = tot++; 31 e[tot].u = v; 32 e[tot].v = u; 33 e[tot].nxt = head[v]; 34 head[v] = tot++; 35 } 36 37 int vis[maxn]; 38 void bfs(int c, int city) 39 { 40 queue<int>que; 41 que.push(c); 42 vis[c] = true; 43 while(!que.empty()){ 44 int now = que.front();que.pop(); 45 for(int ed = head[now]; ed != -1; ed = e[ed].nxt){ 46 if(e[ed].v != city && !vis[e[ed].v]){ 47 vis[e[ed].v] = true; 48 que.push(e[ed].v); 49 } 50 } 51 } 52 return ; 53 } 54 55 int main() 56 { 57 scanf("%d%d%d", &n, &m, &k); 58 memset(head, -1, sizeof(head)); 59 for(int i = 0; i < m; i++){ 60 int u, v; 61 scanf("%d%d", &u, &v); 62 addedge(u, v); 63 } 64 for(int i = 0; i < k; i++){ 65 int city; 66 scanf("%d", &city); 67 memset(vis, 0, sizeof(vis)); 68 int cnt = 0; 69 for(int i = 1; i <= n; i++){ 70 if(!vis[i] && i != city){ 71 bfs(i, city); 72 cnt++; 73 } 74 } 75 printf("%d ", cnt - 1); 76 } 77 return 0; 78 }
思路:大模拟。我好菜系列。
用队列模拟每个窗口排队的人。没满的时候就是从左到右排就行了,满了之后就是哪个队伍先有人走哪个队伍就先进去。这里用优先队列模拟。
坑点是,如果在五点之前被服务了,服务时间超过五点也没关系,但是如果开始服务时间就已经超过五点了就要Sorry。
还有一个地方写错了是刚开始所有人都排进去就直接结束了,但是队列里其实还有人。
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<map> 4 #include<set> 5 #include<iostream> 6 #include<cstring> 7 #include<algorithm> 8 #include<vector> 9 #include<cmath> 10 #include<queue> 11 12 #define inf 0x7fffffff 13 using namespace std; 14 typedef long long LL; 15 typedef pair<int, int> pr; 16 17 int n, m, k, q; 18 const int maxn = 1005; 19 int process[maxn], total[maxn]; 20 queue<int>que[25]; 21 int t[25]; 22 23 struct node{ 24 int time, line; 25 node(){ 26 } 27 node(int t, int l) 28 { 29 time = t; 30 line = l; 31 } 32 bool operator < (const node& a)const{ 33 if(time == a.time)return line > a.line; 34 return time > a.time; 35 } 36 }; 37 38 int main() 39 { 40 scanf("%d%d%d%d", &n, &m, &k, &q); 41 for(int i = 1; i <= k; i++){ 42 scanf("%d", &process[i]); 43 } 44 45 int id = 1; 46 priority_queue<node>lineque; 47 for(int i = 1; i <= m; i++){ 48 for(int j = 1; j <= n; j++){ 49 que[j].push(id++); 50 if(id > k)break; 51 } 52 if(id > k)break; 53 } 54 55 //que[1].push(id++); 56 57 // for(int i = 1; i <= n; i++){ 58 // lineque.push(node(0, i)); 59 // } 60 int sum = k; 61 while(id <= k){ 62 //int linetime = 10000, line; 63 for(int i = 1; i <= n; i++){ 64 if(!que[i].empty()){ 65 int peo = que[i].front();que[i].pop(); 66 sum--; 67 t[i] += process[peo]; 68 total[peo] = t[i]; 69 lineque.push(node(total[peo], i)); 70 } 71 // if(linetime > t[i]){ 72 // linetime = t[i]; 73 // line = i; 74 // } 75 } 76 // que[line].push(id++); 77 78 node l = lineque.top();lineque.pop(); 79 que[l.line].push(id++); 80 } 81 82 while(sum){ 83 for(int i = 1; i <= n; i++){ 84 if(!que[i].empty()){ 85 int peo = que[i].front();que[i].pop(); 86 sum--; 87 t[i] += process[peo]; 88 total[peo] = t[i]; 89 } 90 } 91 } 92 93 for(int i = 0; i < q; i++){ 94 int p; 95 scanf("%d", &p); 96 int h = total[p] / 60; 97 int min = total[p] % 60; 98 //printf("%d:%d ",p,total[p]); 99 if(h + 8 >= 17 && min != 0 && total[p] - process[p] >= 540){ 100 printf("Sorry "); 101 } 102 else{ 103 printf("%02d:%02d ", h + 8, min); 104 } 105 } 106 return 0; 107 }
思路:感觉PAT的题意每次讲的都好不清楚啊【远没有ACM表述清晰严谨】。
题目的意思是一个数10进制下是质数,然后转换成D进制再逆序之后的字符串当成10进制也是质数。
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<map> 4 #include<set> 5 #include<iostream> 6 #include<cstring> 7 #include<algorithm> 8 #include<vector> 9 #include<cmath> 10 #include<stack> 11 #include<queue> 12 13 #define inf 0x7fffffff 14 using namespace std; 15 typedef long long LL; 16 typedef pair<int, int> pr; 17 18 int n, d; 19 const int maxn = 1e6 + 6; 20 bool p[maxn]; 21 22 void isprime() 23 { 24 p[0] = true; 25 p[1] = true; 26 for(int i = 2; i <= maxn; i++){ 27 if(p[i])continue; 28 int j = 2; 29 while(j * i <= maxn){ 30 p[j * i] = true; 31 j++; 32 } 33 } 34 } 35 36 //stack<int>sss; 37 int getnum(int k) 38 { 39 int res = 0, r = 1; 40 int tmp = n; 41 while(tmp){ 42 res += r * (tmp % k); 43 //sss.push(tmp % k); 44 tmp /= k; 45 r *= k; 46 } 47 return res; 48 } 49 50 int getreverse(int k) 51 { 52 int res = 0, tmp = n; 53 while(tmp){ 54 res = res * k + tmp % k; 55 tmp /= k; 56 } 57 return res; 58 } 59 60 int main() 61 { 62 isprime(); 63 while(scanf("%d", &n) != EOF && n >= 0){ 64 scanf("%d", &d); 65 //cout<<getnum()<<endl<<getreverse()<<endl; 66 if(!p[getnum(10)] && !p[getreverse(d)]){ 67 printf("Yes "); 68 } 69 else{ 70 printf("No "); 71 } 72 } 73 74 return 0; 75 }