- /****************##### بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم #####******************
- __________________________________________________________________________
- ###################### Ya-Seen Arafat(ACWizard) #########################
- ###################### UAP-CSE-33B #########################
- *************************************************************************/
- #include <bits/stdc++.h>
- #define sc(n) scanf("%d", &n)
- #define scD(n) scanf("%lf", &n)
- #define scS(n) scanf("%s", &n)
- #define S 103
- using namespace std;
- int n, ar[S*3][S];
- int dp[S*3][S], sz[S*3];
- int MB(int row, int ind, int jump){
- if(row > (n+n-1) || ind < 0 || ind > sz[jump])return 0;
- if(dp[jump][ind] != -1)return dp[jump][ind];
- if(row < n)dp[jump][ind] = (ar[jump][ind] + max(MB(row+1, ind, jump+1), MB(row+1, ind+1, jump+1)));
- else dp[jump][ind] = (ar[jump][ind] + max(MB(row+1, ind-1, jump+1), MB(row+1, ind, jump+1)));
- return dp[jump][ind];
- }
- int main(){
- int t, cs = 0;
- scanf("%d", &t);
- while(t--){
- memset(dp, -1, sizeof(dp));
- memset(ar, 0, sizeof(ar));
- scanf("%d", &n);
- int z;
- for(int i = 0; i < n; i++){
- for(int j = 0; j <= i; j++){
- scanf("%d", &ar[i][j]);
- }
- sz[i] = i;
- z = i;
- }
- int jj = n-1;
- for(int i = z+1; i < (n+n-1); i++){
- for(int j = 0; j < jj; j++){
- scanf("%d", &ar[i][j]);
- }
- sz[i] = jj-1;
- jj--;
- }
- int ans = MB(1, 0, 0);
- cout << "Case " << ++cs << ": " << ans << endl;
- }
- return 0;
- }
Saturday, January 30, 2016
LightOJ - 1004 - Monkey Banana Problem
Saturday, January 9, 2016
LightOJ - 1009 - Back to Underworld --- using BFS
- /****************##### بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم #####******************
- __________________________________________________________________________
- ###################### Ya-Seen Arafat(ACWizard) #########################
- ###################### UAP-CSE-33B #########################
- *************************************************************************/
- #include <bits/stdc++.h>
- #define sc(n) scanf("%d", &n)
- #define scD(n) scanf("%lf", &n)
- #define scS(n) scanf("%s", &n)
- #define S 20003
- #define SS 100003
- using namespace std;
- typedef long long LL;
- vector <int> race[S], edge;
- int ans[S], check[S];
- int BFS(int src){
- int anss = 0;
- queue <int> Q;
- Q.push(src);
- int sz = edge.size();
- ans[src] = 1;
- while(!Q.empty()){
- int x, y;
- x = Q.front();
- check[x]++;
- Q.pop();
- y = race[x].size();
- for(int i = 0; i < y; i++){
- if(!ans[race[x][i]]){
- Q.push(race[x][i]);
- if(ans[x] == 1)ans[race[x][i]] = 2;
- if(ans[x] == 2)ans[race[x][i]] = 1;
- }
- }
- if(Q.empty()){
- int vamp = 0, lyk = 0;
- for(int i = 0; i < S; i++){
- if(ans[i] == 1)vamp++;
- if(ans[i] == 2)lyk++;
- ans[i] = 0;
- }
- anss += max(vamp, lyk);
- for(int i = 0; i < sz; i++){
- if(check[edge[i]] == 1){
- Q.push(edge[i]);
- ans[edge[i]] = 1;
- break;
- }
- }
- }
- }
- return anss;
- }
- void Do(){
- int t, n, u, v, cs = 0;
- sc(t);
- while(t--){
- sc(n);
- memset(ans, 0, sizeof(ans));
- memset(check, 0, sizeof(check));
- for(int i = 0; i < S; i++)race[i].clear();
- edge.clear();
- while(n--){
- sc(u);
- sc(v);
- if(!check[u])check[u] = 1, edge.push_back(u);
- if(!check[v])check[v] = 1, edge.push_back(v);
- race[u].push_back(v);
- race[v].push_back(u);
- }
- int members = BFS(u);
- printf("Case %d: %d\n", ++cs, members);
- }
- }
- int main(){
- ios_base::sync_with_stdio(0); cin.tie(0);
- #ifndef ONLINE_JUDGE
- ///freopen("contest.txt","w",stdout);
- #endif
- Do();
- return 0;
- }
Subscribe to:
Posts (Atom)