两道水题。直接暴力。数据范围都不大,还没有过,等哪天zoj把题目挂出来了再交。
Density of Power Network
题目链接:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5000
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cctype> 6 #include <stack> 7 #include <queue> 8 #include <map> 9 #include <set> 10 #include <vector> 11 #include <cmath> 12 #include <algorithm> 13 #define lson l, m, rt<<1 14 #define rson m+1, r, rt<<1|1 15 using namespace std; 16 typedef long long int LL; 17 const int MAXN = 0x7fffffff; 18 const int MINN = -0x7fffffff; 19 const double eps = 1e-9; 20 const int dir[8][2] = {{0,1},{1,0},{0,-1},{-1,0},{-1,1}, 21 {1,1},{1,-1},{-1,-1}}; 22 23 int edge[502][502], a[502], b[502]; 24 int main(void){ 25 #ifndef ONLINE_JUDGE 26 freopen("zoj5000.in", "r", stdin); 27 #endif 28 int t, n, m, i, j, k; scanf("%d", &t); 29 while (t--) { 30 scanf("%d%d", &n, &m); 31 memset(edge, 0, sizeof(edge)); 32 for (i = 0;i < m; ++i) {scanf("%d", &a[i]); a[i]--;} 33 for (i = 0;i < m; ++i) {scanf("%d", &b[i]); b[i]--;} 34 for (i = 0;i < m; ++i) edge[a[i]][b[i]] = edge[b[i]][a[i]]= 1; 35 k = 0; 36 for (i = 0; i < n; ++i) { 37 for (j = 0; j < n; ++j) { 38 if (edge[i][j] != 0) k++; 39 } 40 } 41 double K = (double)k*1.0/2.0, N = (double)n*1.0; 42 printf("%.3f\n", K/N); 43 } 44 45 return 0; 46 }
这题目当初没读懂题意,也想复杂了。。。关键是样例都没有看完,o(╯□╰)o
刚才交了,这题过了。
Friends
题目链接:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5002
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cctype> 6 #include <stack> 7 #include <queue> 8 #include <map> 9 #include <set> 10 #include <vector> 11 #include <cmath> 12 #include <algorithm> 13 #define lson l, m, rt<<1 14 #define rson m+1, r, rt<<1|1 15 using namespace std; 16 typedef long long int LL; 17 const int MAXN = 0x7fffffff; 18 const int MINN = -0x7fffffff; 19 const double eps = 1e-9; 20 const int dir[8][2] = {{0,1},{1,0},{0,-1},{-1,0},{-1,1}, 21 {1,1},{1,-1},{-1,-1}}; 22 int edge[120][120]; 23 int main(void){ 24 #ifndef ONLINE_JUDGE 25 freopen("zoj5002.in", "r", stdin); 26 #endif 27 int t, n, m, k, i, j, sum, a, b, cnt; 28 bool flag; 29 scanf("%d", &t); 30 while (t--) { 31 scanf("%d%d%d", &n, &m, &k); cnt = 0; 32 memset(edge, 0, sizeof(edge)); 33 for (i = 0; i < m; ++i) { 34 scanf("%d%d", &a, &b); edge[a][b] = edge[b][a] = 1; 35 } 36 flag = false; 37 for (i= 0; i < n; ++i) { 38 for (j = 0; j < n; ++j) { 39 if (i == j) continue; 40 if (edge[i][j] == 1) continue; 41 sum = 0; 42 for (int z = 0; z < n; ++z) { 43 if (edge[i][z]==1 && edge[j][z]==1) sum++; 44 if (sum >= k) break; 45 } 46 if (sum >= k) { 47 edge[i][j] = edge[j][i] = 1; cnt++; 48 i = -1; 49 break; 50 } 51 } 52 if (flag) break; 53 } 54 printf("%d\n", cnt); 55 } 56 57 return 0; 58 }
这题当初真的是因为想复杂了,,,唉,因为范围只有100,所以应该可以暴力吧……所以,以后做题首先注意数据范围!
这题目刚刚交了一下,WA了一次,然后发现一个比较有意思的细节,就是48行,新确定一个关系就要从头开始再找一次,哈哈,还好自己很快就注意到这个东西,不过代码确实比较挫……700+ms过的……