UVa-10895 : Matrix Transpose
問題概要
素な行列の転置行列を指定されたフォーマットで出力する問題。
解法
行列が素なので全部もたないように気をつければ後はやるだけ。
acceptされたコード
#include <cstdio> #include <vector> using namespace std; int N, M; const int MAX_N = 10001; vector< pair<int, int> > mat[MAX_N]; //( 位置、値 ) void init(){ for(int i=1; i<=M; i++){ mat[i].clear(); } for(int i=1; i<=N; i++){ int k; scanf("%d", &k); vector<int> pos(k); for(int j=0; j<k; j++){ int x; scanf("%d", &x); pos[j] = x; } for(int j=0; j<k; j++){ int x; scanf("%d", &x); mat[pos[j]].push_back( make_pair(i, x) ); } } } void solve(){ printf("%d %d\n", M, N); for(int i=1; i<=M; i++){ if(mat[i].empty()){ puts("0"); puts(""); } else{ printf("%d ", (int)mat[i].size()); for(int j=0; j<(int)mat[i].size(); j++){ printf("%d%c", mat[i][j].first, j==(int)mat[i].size()-1 ? '\n' : ' '); } for(int j=0; j<(int)mat[i].size(); j++){ printf("%d%c", mat[i][j].second, j==(int)mat[i].size()-1 ? '\n' : ' '); } } } } int main(){ while(scanf("%d%d", &N, &M) != EOF){ init(); solve(); } return 0; }