We propose a method of learning to determine appropriate roles for forming efficiently teams by self-interested agents in task-oriented domains. Service requests on computer networks have recently been rapidly increasing. To improve the performance of such systems, issues with efficient team formation to do tasks have attracted our interest. The main feature of the proposed method is learning from two-sided viewpoints, i.e., team leaders who have the initiative to form teams or team members who work in one of the teams that are solicited. For this purpose, we introduce three parameters to agents so that they can select their roles of being a leader or a member. Then, an agent can anticipate what other agents should be selected as team members and what team it should join. Our experiments demonstrated that the amount of utility earned as the result of successful team formation was considerably larger than that with a conventional method. We also conducted a number of experiments to investigate the characteristics of the proposed method. The results revealed that the divisional cooperation between agents was developed, which could reduce the chance of conflicts in decisions to play roles and this achieved efficient team formation.